Skip to content

Commit

Permalink
nix flake lock: Fail if there is an unlocked input
Browse files Browse the repository at this point in the history
Since the only purpose of `nix flake lock` is to write a new lock
file, it should be a fatal error if we can't write the lock file.
  • Loading branch information
edolstra committed Dec 9, 2024
1 parent 3081e7c commit 8b9e0f8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/libflake/flake/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ LockedFlake lockFlake(
if (lockFlags.writeLockFile) {
if (sourcePath || lockFlags.outputLockFilePath) {
if (auto unlockedInput = newLockFile.isUnlocked()) {
if (lockFlags.failOnUnlocked)
throw Error("cannot write lock file of flake '%s' because it has an unlocked input ('%s').\n", topRef, *unlockedInput);
if (state.fetchSettings.warnDirty)
warn("will not write lock file of flake '%s' because it has an unlocked input ('%s')", topRef, *unlockedInput);
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/libflake/flake/flake.hh
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ struct LockFlags
*/
bool writeLockFile = true;

/**
* Throw an exception when the flake has an unlocked input.
*/
bool failOnUnlocked = false;

/**
* Whether to use the registries to lookup indirect flake
* references like 'nixpkgs'.
Expand Down
1 change: 1 addition & 0 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ struct CmdFlakeLock : FlakeCommand
settings.tarballTtl = 0;

lockFlags.writeLockFile = true;
lockFlags.failOnUnlocked = true;
lockFlags.applyNixConfig = true;

lockFlake();
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/flakes/unlocked-override.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ git -C "$flake2Dir" add flake.nix
echo 456 > "$flake1Dir"/x.nix

[[ $(nix eval --json "$flake2Dir#x" --override-input flake1 "$TEST_ROOT/flake1") = 456 ]]

expectStderr 1 nix flake lock "$flake2Dir" --override-input flake1 "$TEST_ROOT/flake1" |
grepQuiet "cannot write lock file.*because it has an unlocked input"

0 comments on commit 8b9e0f8

Please sign in to comment.