From 453c3a603f4e6fa3f8c706e73f9869bc7f76c640 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 26 Feb 2021 14:55:54 +0100 Subject: [PATCH] nix flake update: Recreate the lock file This is probably what most people expect it to do. Fixes #3781. There is a new command 'nix flake lock' that has the old behaviour of 'nix flake update', i.e. it just adds missing lock file entries unless overriden using --update-input. --- src/libutil/args.cc | 8 ++++++++ src/libutil/args.hh | 2 ++ src/nix/flake-lock.md | 38 ++++++++++++++++++++++++++++++++++++++ src/nix/flake-update.md | 37 +++++++++---------------------------- src/nix/flake.cc | 34 +++++++++++++++++++++++++++++++++- tests/flakes.sh | 28 ++++++++++++++-------------- 6 files changed, 104 insertions(+), 43 deletions(-) create mode 100644 src/nix/flake-lock.md diff --git a/src/libutil/args.cc b/src/libutil/args.cc index 75eb19d28..afed0670f 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -19,6 +19,14 @@ void Args::addFlag(Flag && flag_) if (flag->shortName) shortFlags[flag->shortName] = flag; } +void Args::removeFlag(const std::string & longName) +{ + auto flag = longFlags.find(longName); + assert(flag != longFlags.end()); + if (flag->second->shortName) shortFlags.erase(flag->second->shortName); + longFlags.erase(flag); +} + void Completions::add(std::string completion, std::string description) { assert(description.find('\n') == std::string::npos); diff --git a/src/libutil/args.hh b/src/libutil/args.hh index 4721c21df..c08ba8abd 100644 --- a/src/libutil/args.hh +++ b/src/libutil/args.hh @@ -140,6 +140,8 @@ public: void addFlag(Flag && flag); + void removeFlag(const std::string & longName); + void expectArgs(ExpectedArg && arg) { expectedArgs.emplace_back(std::move(arg)); diff --git a/src/nix/flake-lock.md b/src/nix/flake-lock.md new file mode 100644 index 000000000..2af0ad81e --- /dev/null +++ b/src/nix/flake-lock.md @@ -0,0 +1,38 @@ +R""( + +# Examples + +* Update the `nixpkgs` and `nix` inputs of the flake in the current + directory: + + ```console + # nix flake lock --update-input nixpkgs --update-input nix + * Updated 'nix': 'github:NixOS/nix/9fab14adbc3810d5cc1f88672fde1eee4358405c' -> 'github:NixOS/nix/8927cba62f5afb33b01016d5c4f7f8b7d0adde3c' + * Updated 'nixpkgs': 'github:NixOS/nixpkgs/3d2d8f281a27d466fa54b469b5993f7dde198375' -> 'github:NixOS/nixpkgs/a3a3dda3bacf61e8a39258a0ed9c924eeca8e293' + ``` + +# Description + +This command updates the lock file of a flake (`flake.lock`) so that +it contains a lock for every flake input specified in +`flake.nix`. Existing lock file entries are not updated unless +required by a flag such as `--update-input`. + +Note that every command that operates on a flake will also update the +lock file if needed, and supports the same flags. Therefore, + +```console +# nix flake lock --update-input nixpkgs +# nix build +``` + +is equivalent to: + +```console +# nix build --update-input nixpkgs +``` + +Thus, this command is only useful if you want to update the lock file +separately from any other action such as building. + +)"" diff --git a/src/nix/flake-update.md b/src/nix/flake-update.md index a2ffedd2a..03b50e38e 100644 --- a/src/nix/flake-update.md +++ b/src/nix/flake-update.md @@ -2,52 +2,33 @@ R""( # Examples -* Update the `nixpkgs` and `nix` inputs of the flake in the current - directory: - - ```console - # nix flake update --update-input nixpkgs --update-input nix - * Updated 'nix': 'github:NixOS/nix/9fab14adbc3810d5cc1f88672fde1eee4358405c' -> 'github:NixOS/nix/8927cba62f5afb33b01016d5c4f7f8b7d0adde3c' - * Updated 'nixpkgs': 'github:NixOS/nixpkgs/3d2d8f281a27d466fa54b469b5993f7dde198375' -> 'github:NixOS/nixpkgs/a3a3dda3bacf61e8a39258a0ed9c924eeca8e293' - ``` - * Recreate the lock file (i.e. update all inputs) and commit the new lock file: ```console - # nix flake update --recreate-lock-file --commit-lock-file + # nix flake update + * Updated 'nix': 'github:NixOS/nix/9fab14adbc3810d5cc1f88672fde1eee4358405c' -> 'github:NixOS/nix/8927cba62f5afb33b01016d5c4f7f8b7d0adde3c' + * Updated 'nixpkgs': 'github:NixOS/nixpkgs/3d2d8f281a27d466fa54b469b5993f7dde198375' -> 'github:NixOS/nixpkgs/a3a3dda3bacf61e8a39258a0ed9c924eeca8e293' … warning: committed new revision '158bcbd9d6cc08ab859c0810186c1beebc982aad' ``` # Description -This command updates the lock file of a flake (`flake.lock`) so that -it contains a lock for every flake input specified in -`flake.nix`. Note that every command that operates on a flake will -also update the lock file if needed, and supports the same -flags. Therefore, +This command recreates the lock file of a flake (`flake.lock`), thus +updating the lock for every mutable input (like `nixpkgs`) to its +current version. This is equivalent to passing `--recreate-lock-file` +to any command that operates on a flake. That is, ```console -# nix flake update --update-input nixpkgs +# nix flake update # nix build ``` is equivalent to: ```console -# nix build --update-input nixpkgs +# nix build --recreate-lock-file ``` -Thus, this command is only useful if you want to update the lock file -separately from any other action such as building. - -> **Note** -> -> This command does *not* update locks that are already present unless -> you explicitly ask for it using `--update-input` or -> `--recreate-lock-file`. Thus, if the lock file already has locks for -> every input, then `nix flake update` (without arguments) does -> nothing. - )"" diff --git a/src/nix/flake.cc b/src/nix/flake.cc index b9cde5d6d..2f0c468a8 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -104,6 +104,14 @@ struct CmdFlakeUpdate : FlakeCommand return "update flake lock file"; } + CmdFlakeUpdate() + { + /* Remove flags that don't make sense. */ + removeFlag("recreate-lock-file"); + removeFlag("update-input"); + removeFlag("no-update-lock-file"); + } + std::string doc() override { return @@ -113,7 +121,30 @@ struct CmdFlakeUpdate : FlakeCommand void run(nix::ref store) override { - /* Use --refresh by default for 'nix flake update'. */ + settings.tarballTtl = 0; + + lockFlags.recreateLockFile = true; + + lockFlake(); + } +}; + +struct CmdFlakeLock : FlakeCommand +{ + std::string description() override + { + return "create missing lock file entries"; + } + + std::string doc() override + { + return + #include "flake-lock.md" + ; + } + + void run(nix::ref store) override + { settings.tarballTtl = 0; lockFlake(); @@ -1006,6 +1037,7 @@ struct CmdFlake : NixMultiCommand CmdFlake() : MultiCommand({ {"update", []() { return make_ref(); }}, + {"lock", []() { return make_ref(); }}, {"info", []() { return make_ref(); }}, {"list-inputs", []() { return make_ref(); }}, {"check", []() { return make_ref(); }}, diff --git a/tests/flakes.sh b/tests/flakes.sh index 2b7bcdd68..25ba2ac43 100644 --- a/tests/flakes.sh +++ b/tests/flakes.sh @@ -232,7 +232,7 @@ nix build -o $TEST_ROOT/result --flake-registry file:///no-registry.json $flake2 nix build -o $TEST_ROOT/result --no-registries $flake2Dir#bar --refresh # Updating the flake should not change the lockfile. -nix flake update $flake2Dir +nix flake lock $flake2Dir [[ -z $(git -C $flake2Dir diff master) ]] # Now we should be able to build the flake in pure mode. @@ -354,10 +354,10 @@ nix build -o $TEST_ROOT/result flake3#xyzzy flake3#fnord nix build -o $TEST_ROOT/result flake4#xyzzy # Test 'nix flake update' and --override-flake. -nix flake update $flake3Dir +nix flake lock $flake3Dir [[ -z $(git -C $flake3Dir diff master) ]] -nix flake update $flake3Dir --recreate-lock-file --override-flake flake2 nixpkgs +nix flake update $flake3Dir --override-flake flake2 nixpkgs [[ ! -z $(git -C $flake3Dir diff master) ]] # Make branch "removeXyzzy" where flake3 doesn't have xyzzy anymore @@ -389,7 +389,7 @@ cat > $flake3Dir/flake.nix < $flake3Dir/flake.nix < $flake3Dir/flake.nix < $flake3Dir/flake.nix < $flake3Dir/flake.nix < $flake3Dir/flake.nix < $flake3Dir/flake.nix < $flake3Dir/flake.nix < $flake3Dir/flake.nix <