Skip to content

nix build: add --print-out-paths flag#6259

Merged
thufschmitt merged 3 commits intoNixOS:masterfrom
Artturin:nixbuildaddprintstorepaths
Apr 22, 2022
Merged

nix build: add --print-out-paths flag#6259
thufschmitt merged 3 commits intoNixOS:masterfrom
Artturin:nixbuildaddprintstorepaths

Conversation

@Artturin
Copy link
Member

@Artturin Artturin commented Mar 14, 2022

has the same functionality as default nix-build

$ nix-build . -A "bash" -A "bash.dev" -A "tinycc"
/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12
/nix/store/c49i1ggnr5cc8gxmk9xm0cn961z104dn-bash-5.1-p12-dev
/nix/store/dbapb08862ajgaax3621fz8hly9fdah3-tcc-0.9.27+date=2022-01-11

$ nix-build . -A "bash"
/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12

$ $HOME/nixgits/nix/result/bin/nix build "nixpkgs#bash" "nixpkgs#bash.dev" "nixpkgs#tinycc" --print-store-paths
/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12
/nix/store/c49i1ggnr5cc8gxmk9xm0cn961z104dn-bash-5.1-p12-dev
/nix/store/dbapb08862ajgaax3621fz8hly9fdah3-tcc-0.9.27+date=2022-01-11

$ $HOME/nixgits/nix/result/bin/nix build "nixpkgs#bash" --print-store-paths
/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12

Supersedes #2622
Closes #5601 #1930

view the first commit for code changes.

@Mic92 @makefu @mickours @domenkozar @fzakaria @tomberek @edolstra

this was a really useful feature of nix-build and may be the reason why scripts haven't switched over to nix build
it would be really really useful to have this feature in nix build

one of my use cases

font=$(nix-build '<nixos>' -A liberation_ttf)/share/fonts/truetype/LiberationSans-Regular.ttf
mogrify -font $font -pointsize 50 -fill white -draw 'text 100,100'$COLOR /tmp/tmpcolor.png

i wasn't sure where to put the test so i just put it in the remote build test

@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch 2 times, most recently from d36e201 to 1cafd84 Compare March 15, 2022 03:16
@Artturin
Copy link
Member Author

updated todo's

@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch from 1cafd84 to 29dc8a3 Compare March 15, 2022 03:21
@06kellyjac
Copy link
Member

yeah, it'd be nice to not have to output json into jq 👍

@edolstra
Copy link
Member

From the CLI guidelines:

When needed, commands should offer a --json flag to allow users to easily parse and script the CLI.

It was a conscious decision not to have ad-hoc, non-extensible, unstructured output on stdout in the new CLI. But I realize it's annoying to have to rely on a tool like jq...

@06kellyjac
Copy link
Member

Yeah we probably shouldn't add flags for every output option under the sun, the json output is very useful. But since this was the previous behavior of nix-build and came in handy I'd say it's worth it.

Or maybe we could consider having jq style templating build in. Then you can do about anything without a 2nd tool:
--template '.[].outputs.out'
like kubectl has a json-template or go-template output format
kubectl get pods --selector=app=cassandra -o jsonpath='{.items[*].metadata.labels.version}'
kubectl get secret my-secret -o go-template='{{range $k,$v := .data}}{{"### "}}{{$k}}{{"\n"}}{{$v|base64decode}}{{"\n\n"}}{{end}}'

@ncfavier
Copy link
Member

I don't think adding JSON filtering to nix is necessary. --print-store-paths (or --print-out-paths) and --json should be enough for most purposes.

@Artturin
Copy link
Member Author

I agree

@06kellyjac
Copy link
Member

Github cli has a --jq option which doesn't seem to depend on jq being installed because jq isn't in the deps

It uses a go library that re-implements jq

github.com/itchyny/gojq

@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch 2 times, most recently from 0b8a9b4 to e13cb0d Compare March 16, 2022 18:18
@Artturin
Copy link
Member Author

added a release note

@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch from e13cb0d to 0757d76 Compare March 18, 2022 17:07
@Artturin
Copy link
Member Author

any code reviews? 🙂

@ncfavier
Copy link
Member

ncfavier commented Mar 18, 2022

Not a code review (well, I looked at it but don't have anything to add), but the obligatory bikeshedding over naming: I'd prefer --print-out-paths. I think it's more precise, and fits better with --out-link and drv.outPath. --print-store-paths sounds like it could mean "print all evaluated store paths" or "all fetched store paths" or something else.

@tomberek
Copy link
Contributor

tomberek commented Mar 19, 2022

“ flag to print the resulting store paths”.

I’m not sure what this means. What is the expectation people can rely on? (This question may also apply to the JSON output.) What will the output contain if combined with remote builders/ eval stores/substitution/symlink generated. What invariant is provided? Eg: Given identical starting stores where one does the build and the other only gets this stdout - what steps can be taken to reach the same Store state, or similar? Does (build + GC ~= substitute with stdout paths)?

The common use case I presume is as a CLI ad-hoc variant of a post-build-hook. What are the semantics compared to this pseudo code:

nix build —option post-build-hook ‘printf “%s\n” $OUT_PATHS’

My intention is not to support or oppose, but to ensure there is consistency so we can apply it in more situations or to understand the distinctions.

@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch from 0757d76 to 5870143 Compare March 19, 2022 18:21
@Artturin Artturin changed the title nix build: add --print-store-paths flag nix build: add --print-output-paths flag Mar 19, 2022
@Artturin
Copy link
Member Author

@tomberek

> $HOME/nixgits/nix/result/bin/nix build --option builders 'ssh://eu.nixbuild.net' -j0 nixpkgsMaster#bash --print-output-paths
/nix/store/dndi916j6yxzfzzj2sma2llhrlwahq06-bash-5.1-p16

> $HOME/nixgits/nix/result/bin/nix build --option builders 'ssh://eu.nixbuild.net' -j0 /nix/store/dndi916j6yxzfzzj2sma2llhrlwahq06-bash-5.1-p16 --print-output-paths
/nix/store/dndi916j6yxzfzzj2sma2llhrlwahq06-bash-5.1-p16

> $HOME/nixgits/nix/result/bin/nix build --option builders 'ssh://eu.nixbuild.net' -j0 nixpkgsMaster#bash.dev --print-output-paths
/nix/store/lrrv3fzii19il2lsqdcb5d9mia2wxy5r-bash-5.1-p16-dev



> $HOME/nixgits/nix/result/bin/nix build nixpkgsMaster#bash --print-output-paths
/nix/store/dndi916j6yxzfzzj2sma2llhrlwahq06-bash-5.1-p16

> $HOME/nixgits/nix/result/bin/nix build /nix/store/dndi916j6yxzfzzj2sma2llhrlwahq06-bash-5.1-p16 --print-output-paths
/nix/store/dndi916j6yxzfzzj2sma2llhrlwahq06-bash-5.1-p16

> $HOME/nixgits/nix/result/bin/nix build nixpkgsMaster#bash.dev --print-output-paths
/nix/store/lrrv3fzii19il2lsqdcb5d9mia2wxy5r-bash-5.1-p16-dev

@Artturin
Copy link
Member Author

Artturin commented Mar 22, 2022

there's a difference when the derivation is built for the first time / rebuilt

nix-build -A bash --check
/nix/store/dndi916j6yxzfzzj2sma2llhrlwahq06-bash-5.1-p16
$HOME/nixgits/nix/result/bin/nix build ".#bash" --print-output-paths --rebuild
/nix/store/lrrv3fzii19il2lsqdcb5d9mia2wxy5r-bash-5.1-p16-dev
/nix/store/s6bspl3x0s8h626vc4ikgv1h8sphlm17-bash-5.1-p16-doc
/nix/store/g0n1a3vdx64zkkprfl0y8643320bsb0a-bash-5.1-p16-info
/nix/store/ddgnym0a3nl1ggz50gg8q1cs0arzic87-bash-5.1-p16-man
/nix/store/dndi916j6yxzfzzj2sma2llhrlwahq06-bash-5.1-p16

nix build --json has the same behaviour currently

@Artturin
Copy link
Member Author

Artturin commented Mar 22, 2022

with nix build when a derivation is built for the first time / rebuilt then all the outputs of it will be symlinked to the current dir

while with nix-build only the main output is symlinked to the current dir

@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch 2 times, most recently from abab65f to 286f109 Compare April 3, 2022 23:00
@Artturin
Copy link
Member Author

Artturin commented Apr 3, 2022

i rebuilt my system with

(self: super: {
  nixUnstable = super.nixUnstable.overrideAttrs (old: {
    src = super.fetchFromGitHub {
      owner = "nixos";
      repo = "nix";
      rev = "abab65f774169c4ebf58823af2a3c327a560deee";
      sha256 = "sha256-kFsJ/WsopJIYKHBpnVGuyXlqdwcrezvvmbT2ycN89r0=";
    };
  });
})

i then ran

nix build "nixpkgs#bash.dev" --print-output-paths --rebuild
/nix/store/c49i1ggnr5cc8gxmk9xm0cn961z104dn-bash-5.1-p12-dev

and

nix build "nixpkgs#bash" --print-output-paths --rebuild
/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12

both work

Thank you @edolstra !!

@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch from 286f109 to 4c06e0e Compare April 5, 2022 06:54
@erikarvstedt
Copy link
Member

I agree with @ncfavier that this should be renamed to --print-out-paths, analogous to --out-link.

@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch 2 times, most recently from 1c1345e to e5b49b6 Compare April 5, 2022 09:37
@edolstra
Copy link
Member

edolstra commented Apr 5, 2022

Alternatively, maybe --out-link should be renamed to --output-link, since there is no good reason why we're abbreviating "output"...

@bjornfor
Copy link
Contributor

bjornfor commented Apr 5, 2022

Alternatively, maybe --out-link should be renamed to --output-link, since there is no good reason why we're abbreviating "output"...

The variable inside builders is called $out not $output though. (Not arguing against rename, just saying.)

@Artturin Artturin changed the title nix build: add --print-output-paths flag nix build: add --print-out-paths flag Apr 7, 2022
@Artturin
Copy link
Member Author

Artturin commented Apr 9, 2022

print-out-paths sounds good to me

Copy link
Contributor

@tomberek tomberek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

functions and runs as expected. Consistency makes me wonder if any other commands should respect this output format. nix copy? nix profile?

@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch from e5b49b6 to bbbf8a0 Compare April 11, 2022 17:54
@Artturin
Copy link
Member Author

resolved conflict

@Artturin Artturin requested review from edolstra and tomberek April 14, 2022 19:27
has the same functionality as default nix-build

$ nix-build . -A "bash" -A "bash.dev" -A "tinycc"
/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12
/nix/store/c49i1ggnr5cc8gxmk9xm0cn961z104dn-bash-5.1-p12-dev
/nix/store/dbapb08862ajgaax3621fz8hly9fdah3-tcc-0.9.27+date=2022-01-11

$ nix-build . -A "bash"
/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12

$ $HOME/nixgits/nix/result/bin/nix build "nixpkgs#bash" "nixpkgs#bash.dev" "nixpkgs#tinycc" --print-out-paths
/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12
/nix/store/c49i1ggnr5cc8gxmk9xm0cn961z104dn-bash-5.1-p12-dev
/nix/store/dbapb08862ajgaax3621fz8hly9fdah3-tcc-0.9.27+date=2022-01-11

$ $HOME/nixgits/nix/result/bin/nix build "nixpkgs#bash" --print-out-paths
/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12
@Artturin Artturin force-pushed the nixbuildaddprintstorepaths branch from bbbf8a0 to 51cfea8 Compare April 20, 2022 16:35
@Artturin
Copy link
Member Author

conflict resolved.

Copy link
Member

@thufschmitt thufschmitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, sorry for the long delay :)

I’ve resolved the small conflict. Waiting for #6437 to be merged so that we can have a green CI, and I’ll merge after that

@thufschmitt thufschmitt merged commit 7f814d6 into NixOS:master Apr 22, 2022
@Artturin Artturin deleted the nixbuildaddprintstorepaths branch April 22, 2022 13:35
@AleXoundOS
Copy link

Sorry, but can it be explained why is --print-out-paths not the default behavior? Are there any drawbacks for printing output paths by default for anybody?

@edolstra
Copy link
Member

edolstra commented Jun 7, 2022

Because we don't want the new CLI to be spammy by default. Also, the CLI guideline is to use --json for machine-parseable output rather than ad hoc formats like --print-out-paths.

@AleXoundOS
Copy link

AleXoundOS commented Jun 7, 2022

After years of quickly digging into just-built out paths, using in scripts, etc and seeing no problem with it. And the number of the built out paths is rarely > 5.
It can be really hard to comprehend it, honestly. The only hope is that this will make nix look less "geeky" for newcomers. But the rhetoric question - is nix generally ready to become so user-friendly, that the built out paths information is that much irrelevant to be suppressed? Or a new CLI is going to take credits for a fantastic nix breakthrough 🚀. Thank you for your work.

@rockboynton
Copy link

Is there a way to do this via nix copy as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add --print option to nix build to print the store path