diff --git a/src/nix/home-apply.md b/src/nix/home-apply.md new file mode 100644 index 000000000..d3a39b803 --- /dev/null +++ b/src/nix/home-apply.md @@ -0,0 +1,9 @@ +R""( + +# Description + +This command activates a home-manager configuration. + +)"" + + diff --git a/src/nix/home-build.md b/src/nix/home-build.md new file mode 100644 index 000000000..2040dcf20 --- /dev/null +++ b/src/nix/home-build.md @@ -0,0 +1,7 @@ +R""( + +# Description + +This command builds NixOS configurations. The default attribute name is the current system hostname. + +)"" diff --git a/src/nix/home.cc b/src/nix/home.cc new file mode 100644 index 000000000..ab97d255e --- /dev/null +++ b/src/nix/home.cc @@ -0,0 +1,123 @@ +#include "command.hh" +#include "activatables.hh" +#include "progress-bar.hh" + +using namespace nix; + +void execute(std::string program, Strings args) { + stopProgressBar(); + restoreProcessContext(); + args.push_front(program); + execvp(program.c_str(), stringsToCharPtrs(args).data()); + + throw SysError("unable to execute '%s'", program); +} + +struct HomeCommand : ActivatableCommand +{ + HomeCommand() + : ActivatableCommand("activationPackage") + {} + + Strings getDefaultFlakeAttrPaths() override + { + auto username = getUserName(); + char hostname[1024]; + hostname[1023] = '\0'; + gethostname(hostname, 1024); + + Strings res{ + "homeConfigurations." + username, + "homeConfigurations." + username + "@" + std::string(hostname), + }; + return res; + } + + Strings getDefaultFlakeAttrPathPrefixes() override + { + Strings res{"homeConfigurations."}; + return res; + } + +}; + +struct HomeActivationCommand : ActivationCommand +{ + HomeActivationCommand(std::string activationType, std::string selfCommandName) + : ActivationCommand(activationType, selfCommandName) + { } + + void run(nix::ref store) override + { + auto out = buildActivatable(store); + execute(store->printStorePath(out) + "/activate", Strings{}); + } +}; + +struct CmdHomeBuild : ActivatableBuildCommand +{ + std::string description() override + { + return "build a home-manager configuration"; + } + + std::string doc() override + { + return + #include "home-build.md" + ; + } +}; + +struct CmdHomeApply : HomeActivationCommand +{ + CmdHomeApply() : HomeActivationCommand("switch", "apply") + { + } + + std::string description() override + { + return "activate a home-manager configuration"; + } + + std::string doc() override + { + return + #include "home-apply.md" + ; + } +}; + +struct CmdHome : NixMultiCommand +{ + CmdHome() + : MultiCommand({ + {"build", []() { return make_ref(); }}, + {"apply", []() { return make_ref(); }}, + }) + { + } + + std::string description() override + { + return "manage home-manager configurations"; + } + + std::string doc() override + { + return + #include "home.md" + ; + } + + void run() override + { + if (!command) + throw UsageError("'nix home' requires a sub-command."); + settings.requireExperimentalFeature(Xp::NixCommand); + command->second->prepare(); + command->second->run(); + } +}; + +static auto rCmdHome = registerCommand("home"); diff --git a/src/nix/home.md b/src/nix/home.md new file mode 100644 index 000000000..e2042aeb5 --- /dev/null +++ b/src/nix/home.md @@ -0,0 +1,7 @@ +R""( + +# Description + +This is a group of commands for managing home-manager configurations. + +)""