58 lines
2.4 KiB
Nix
58 lines
2.4 KiB
Nix
# Passwordless privilege escalation for Colmena deployments, scoped to
|
|||
|
|
# exactly the two commands colmena executes on a non-root target host
|
||
|
|
# (colmena src/nix/host/ssh.rs + profile.rs, verified for 0.5/0.6):
|
||
|
|
#
|
||
|
|
# sudo -H -- nix-env --profile /nix/var/nix/profiles/system --set /nix/store/<toplevel>
|
||
|
|
# sudo -H -- /nix/store/<toplevel>/bin/switch-to-configuration <switch|boot|test|dry-activate>
|
||
|
|
#
|
||
|
|
# Colmena connects with BatchMode=yes and without a TTY, so a sudo
|
||
|
|
# password prompt can never be answered — non-root deploys REQUIRE
|
||
|
|
# NOPASSWD for these two invocations. Everything else the user runs via
|
||
|
|
# sudo still asks for a password: the exception covers only these exact
|
||
|
|
# command shapes.
|
||
|
|
#
|
||
|
|
# Residual risk (inherent to any NixOS remote activation): whoever can
|
||
|
|
# run these commands can activate any closure present in the store as
|
||
|
|
# the new system. That is the deploy capability itself, not an
|
||
|
|
# unintended side effect.
|
||
|
|
#
|
||
|
|
# NOTE: if colmena is invoked with extra nix flags (e.g. --show-trace,
|
||
|
|
# --option cores N) the nix-env argv grows trailing flags and no longer
|
||
|
|
# matches the pinned rule — plain `colmena apply` is what this covers.
|
||
|
|
{
|
||
|
|
config,
|
||
|
|
lib,
|
||
|
|
...
|
||
|
|
}: let
|
||
|
|
deployUser = "m3tam3re";
|
||
|
|
in
|
||
|
|
with lib; {
|
||
|
|
options.extraServices.colmenaDeploy.enable =
|
||
|
|
mkEnableOption "passwordless colmena deployment (sudo NOPASSWD scoped to the system profile switch and switch-to-configuration activation only)";
|
||
|
|
|
||
|
|
config = mkIf config.extraServices.colmenaDeploy.enable {
|
||
|
|
security.sudo.extraRules = [
|
||
|
|
{
|
||
|
|
users = [deployUser];
|
||
|
|
commands =
|
||
|
|
# Activate a store closure. Only the four goals colmena
|
||
|
|
# passes, as the sole argument, are allowed.
|
||
|
|
map (goal: {
|
||
|
|
command = "/nix/store/*/bin/switch-to-configuration ${goal}";
|
||
|
|
options = ["NOPASSWD"];
|
||
|
|
}) ["boot" "switch" "test" "dry-activate"]
|
||
|
|
++ [
|
||
|
|
{
|
||
|
|
# Make a store closure the system profile (goals
|
||
|
|
# switch/boot). Over SSH "nix-env" deterministically
|
||
|
|
# resolves to /run/current-system/sw/bin/nix-env — it
|
||
|
|
# only ships in the system closure — which is the path
|
||
|
|
# sudo matches here.
|
||
|
|
command = "/run/current-system/sw/bin/nix-env --profile /nix/var/nix/profiles/system --set /nix/store/*";
|
||
|
|
options = ["NOPASSWD"];
|
||
|
|
}
|
||
|
|
];
|
||
|
|
}
|
||
|
|
];
|
||
|
|
};
|
||
|
|
}
|