From 42f9da1ffcca2d6919002bf8fadb7b1a7071e0ee Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 7 Jan 2023 15:27:00 +0100 Subject: [PATCH] introduce CmdSystemApply, CmdSystemBoot --- src/nix/system-apply.md | 15 +++++++++ src/nix/system-boot.md | 16 +++++++++ src/nix/system.cc | 73 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/nix/system-apply.md create mode 100644 src/nix/system-boot.md diff --git a/src/nix/system-apply.md b/src/nix/system-apply.md new file mode 100644 index 000000000..df6c82167 --- /dev/null +++ b/src/nix/system-apply.md @@ -0,0 +1,15 @@ +R""( + +# Description + +This command activates NixOS configurations and sets it as the default boot configuration. + +The `profile` parameter is used to specify the system profile to use. + +The default system profile is `/nix/var/nix/profiles/system`. + +A system profile named `hello` would be located at `/nix/var/nix/profiles/system-profiles/hello`. + +)"" + + diff --git a/src/nix/system-boot.md b/src/nix/system-boot.md new file mode 100644 index 000000000..3214baa9f --- /dev/null +++ b/src/nix/system-boot.md @@ -0,0 +1,16 @@ +R""( + +# Description + +This command sets the given NixOS configuration as the default boot configuration. It will be activated on the next boot. + +The `profile` parameter is used to specify the system profile to use. + +The default system profile is `/nix/var/nix/profiles/system`. + +A system profile named `hello` would be located at `/nix/var/nix/profiles/system-profiles/hello`. + +)"" + + + diff --git a/src/nix/system.cc b/src/nix/system.cc index 4c3fe59b1..999f1c7d3 100644 --- a/src/nix/system.cc +++ b/src/nix/system.cc @@ -91,6 +91,35 @@ struct SystemCommand : InstallableCommand } }; +struct SystemInstalledActivationCommand : SystemCommand, MixProfile +{ + std::string activationType; + + void run(nix::ref store) override + { + std::vector> installableContext; + auto state = getEvalState(); + installableContext.emplace_back(transformInstallable(state, installable)); + auto buildables = Installable::build( + getEvalStore(), store, + Realise::Outputs, + installableContext, bmNormal); + + if (!profile) { + profile = settings.nixStateDir + "/profiles/system"; + } else { + profile = settings.nixStateDir + "/profiles/system-profiles/" + profile.value(); + } + + BuiltPaths buildables2; + for (auto & b : buildables) + buildables2.push_back(b.path); + updateProfile(buildables2); + + executePrivileged(profile.value() + "/bin/switch-to-configuration", Strings{activationType}); + } +}; + struct CmdSystemBuild : SystemCommand, MixDryRun { Path outLink = "result"; @@ -228,12 +257,56 @@ struct CmdSystemActivate : SystemCommand, MixDryRun } }; +struct CmdSystemApply : SystemInstalledActivationCommand +{ + std::string activationType = "switch"; + + CmdSystemApply() + { + } + + std::string description() override + { + return "activate a NixOS system configuration and make it the default boot configuration"; + } + + std::string doc() override + { + return + #include "system-apply.md" + ; + } +}; + +struct CmdSystemBoot : SystemInstalledActivationCommand +{ + std::string activationType = "boot"; + + CmdSystemBoot() + { + } + + std::string description() override + { + return "make a NixOS configuration the default boot configuration"; + } + + std::string doc() override + { + return + #include "system-boot.md" + ; + } +}; + struct CmdSystem : NixMultiCommand { CmdSystem() : MultiCommand({ {"build", []() { return make_ref(); }}, {"activate", []() { return make_ref(); }}, + {"apply", []() { return make_ref(); }}, + {"boot", []() { return make_ref(); }}, }) { }