Skip to content

Do not attempt to write a lock file in builtins.getFlake#6544

Merged
thufschmitt merged 1 commit into
NixOS:masterfrom
ncfavier:getFlake-no-write-lock-file
May 18, 2022
Merged

Do not attempt to write a lock file in builtins.getFlake#6544
thufschmitt merged 1 commit into
NixOS:masterfrom
ncfavier:getFlake-no-write-lock-file

Conversation

@ncfavier

Copy link
Copy Markdown
Member

Fixes #6541

@thufschmitt

Copy link
Copy Markdown
Member

I don’t think we can just do that, because that would break purity (since getFlake can be used in pure eval mode, and locking a flake is inherently impure). But it’s probably fine to set writeLockFile to !evalSettings.pureEval, so that it’s only available in impure mode.

For the pure use-case, maybe the interface of getFlake could be extended (to allow passing an external lockfile for example). Alternatively/in the meantime, fetchTree+callLocklessFlake might do the job too

@ncfavier

Copy link
Copy Markdown
Member Author

This should be covered by the lines above:

if (evalSettings.pureEval && !flakeRef.input.isLocked())
throw Error("cannot call 'getFlake' on unlocked flake reference '%s', at %s (use --impure to override)", flakeRefS, state.positions[pos]);

@edolstra

Copy link
Copy Markdown
Member

@ncfavier That only checks whether the flake reference is locked (e.g. github:nix-community/home-manager/32a7da69dc53c9eb5ad0675eb7fdc58f7fe35272 vs github:nix-community/home-manager). A locked flake reference can refer to a flake that doesn't have a flake.lock, which would be non-reproducible (e.g. its flake.nix can reference an unlocked nixpkgs).

@thufschmitt

Copy link
Copy Markdown
Member

Ah I should have looked better. The line you point to isn’t enough (it only checks whether the ref passed to getFlake is a locked reference, not that it contains a lockfile), but as @edolstra mentionned in #6541 (comment), passing .allowMutable = !evalSettings.pureEval prevents it from updating the lockfile in pure mode. So it’s all right :)

@thufschmitt

Copy link
Copy Markdown
Member

So it’s all right :)

Well, it’s all right, bit given how non-trivial it is, I think it’s definitely worth a test to ensure that it doesn’t get accidentally broken ;)

The whole thing actually deserves some tests imho

@ncfavier

Copy link
Copy Markdown
Member Author

There seems to already be one:

nix/tests/flakes.sh

Lines 158 to 161 in 51d13c4

# Building a flake with an unlocked dependency should fail in pure mode.
(! nix build -o $TEST_ROOT/result flake2#bar --no-registries)
(! nix build -o $TEST_ROOT/result flake2#bar --no-use-registries)
(! nix eval --expr "builtins.getFlake \"$flake2Dir\"")

Can you think of other scenarios to test?

@ncfavier ncfavier force-pushed the getFlake-no-write-lock-file branch from 681fc3c to 169384a Compare May 18, 2022 13:45
@ncfavier

Copy link
Copy Markdown
Member Author

Added a test for the case that this PR fixes

@thufschmitt thufschmitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Added a test for the case that this PR fixes

Thanks :)

Can you think of other scenarios to test?

Nope, should be good since the impure case is already tested :)

Thanks for that then!

@thufschmitt thufschmitt merged commit 78dc64e into NixOS:master May 18, 2022
@ncfavier ncfavier deleted the getFlake-no-write-lock-file branch May 18, 2022 14:41
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.

builtins.getFlake fails with "cannot write modified lock file"

3 participants