2022-04-03 19:01:21 +01:00
|
|
|
{ lib, pkgs, config, systems, ... }:
|
2022-02-19 22:55:53 +00:00
|
|
|
let
|
2022-04-03 19:01:21 +01:00
|
|
|
inherit (builtins) head attrNames;
|
2022-05-16 00:05:02 +01:00
|
|
|
inherit (lib) mkMerge mkIf mkDefault optionalAttrs mapAttrs' optionalString;
|
2022-02-19 22:55:53 +00:00
|
|
|
inherit (lib.my) mkOpt' mkBoolOpt';
|
|
|
|
|
|
|
|
cfg = config.my.deploy;
|
2022-04-03 19:01:21 +01:00
|
|
|
|
2022-05-16 00:05:02 +01:00
|
|
|
# Based on https://github.com/serokell/deploy-rs/blob/master/flake.nix
|
|
|
|
nixosActivate = mode: base: (pkgs.deploy-rs.lib.activate.custom // { dryActivate = "$PROFILE/bin/switch-to-configuration dry-activate"; }) base.config.system.build.toplevel ''
|
|
|
|
# work around https://github.com/NixOS/nixpkgs/issues/73404
|
|
|
|
cd /tmp
|
|
|
|
|
|
|
|
$PROFILE/bin/switch-to-configuration ${mode}
|
|
|
|
|
|
|
|
# https://github.com/serokell/deploy-rs/issues/31
|
|
|
|
${with base.config.boot.loader;
|
|
|
|
optionalString ((mode == "switch" || mode == "boot") && systemd-boot.enable)
|
|
|
|
"sed -i '/^default /d' ${efi.efiSysMountPoint}/loader/loader.conf"}
|
|
|
|
'';
|
|
|
|
|
2022-04-18 15:46:38 +01:00
|
|
|
ctrProfiles = optionalAttrs cfg.generate.containers.enable (mapAttrs' (n: c:
|
|
|
|
let
|
|
|
|
ctrConfig = systems."${n}".configuration.config;
|
|
|
|
in
|
|
|
|
{
|
2022-04-03 19:01:21 +01:00
|
|
|
name = "container-${n}";
|
|
|
|
value = {
|
2022-04-18 15:46:38 +01:00
|
|
|
path = pkgs.deploy-rs.lib.activate.custom ctrConfig.my.buildAs.container
|
2022-05-31 21:25:51 +01:00
|
|
|
(if c.hotReload then ''
|
|
|
|
if systemctl show -p StatusText systemd-nspawn@${n} | grep -q "Dummy container"; then
|
|
|
|
action=restart
|
|
|
|
else
|
|
|
|
action=reload
|
|
|
|
fi
|
|
|
|
|
|
|
|
systemctl "$action" systemd-nspawn@${n}
|
|
|
|
'' else "systemctl restart systemd-nspawn@${n}");
|
2022-04-03 19:01:21 +01:00
|
|
|
profilePath = "/nix/var/nix/profiles/per-container/${n}/system";
|
|
|
|
|
|
|
|
user = "root";
|
|
|
|
};
|
|
|
|
}) config.my.containers.instances);
|
2022-02-19 22:55:53 +00:00
|
|
|
in
|
|
|
|
{
|
2022-02-20 20:16:49 +00:00
|
|
|
options.my.deploy = with lib.types; {
|
2022-02-19 22:55:53 +00:00
|
|
|
authorizedKeys = {
|
|
|
|
keys = mkOpt' (listOf singleLineStr) [ ] "SSH public keys to add to the default deployment user.";
|
2022-02-22 00:59:57 +00:00
|
|
|
keyFiles = mkOpt' (listOf path) [ lib.my.sshKeyFiles.deploy ] "SSH public key files to add to the default deployment user.";
|
2022-02-19 22:55:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enable = mkBoolOpt' true "Whether to expose deploy-rs configuration for this system.";
|
2022-02-20 20:16:49 +00:00
|
|
|
inherit (lib.my.deploy-rs) node;
|
2022-02-19 22:55:53 +00:00
|
|
|
|
|
|
|
generate = {
|
2022-05-16 00:05:02 +01:00
|
|
|
system = {
|
|
|
|
enable = mkBoolOpt' true "Whether to generate a deploy-rs profile for this system's config.";
|
|
|
|
mode = mkOpt' str "switch" "switch-to-configuration mode.";
|
|
|
|
};
|
2022-04-03 19:01:21 +01:00
|
|
|
containers.enable = mkBoolOpt' true "Whether to generate deploy-rs profiles for this system's containers.";
|
2022-02-19 22:55:53 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkMerge [
|
|
|
|
{
|
2022-02-20 20:16:49 +00:00
|
|
|
my.deploy.enable = mkIf config.my.build.isDevVM false;
|
|
|
|
}
|
|
|
|
(mkIf cfg.enable {
|
|
|
|
my.deploy.node = {
|
|
|
|
hostname = mkDefault config.networking.fqdn;
|
2022-04-03 19:01:21 +01:00
|
|
|
profilesOrder = [ "system" ] ++ (attrNames ctrProfiles);
|
2022-02-20 20:16:49 +00:00
|
|
|
profiles = {
|
|
|
|
system = mkIf cfg.generate.system.enable {
|
2022-05-16 00:05:02 +01:00
|
|
|
path = nixosActivate cfg.generate.system.mode { inherit config; };
|
2022-02-19 22:55:53 +00:00
|
|
|
|
2022-02-20 20:16:49 +00:00
|
|
|
user = "root";
|
2022-02-19 22:55:53 +00:00
|
|
|
};
|
2022-04-03 19:01:21 +01:00
|
|
|
} // ctrProfiles;
|
2022-02-20 20:16:49 +00:00
|
|
|
|
|
|
|
sshUser = "deploy";
|
|
|
|
user = mkDefault "root";
|
|
|
|
sudo = mkDefault (if config.security.doas.enable then "doas -u" else "sudo -u");
|
|
|
|
sshOpts = mkDefault [ "-p" (toString (head config.services.openssh.ports)) ];
|
2022-02-19 22:55:53 +00:00
|
|
|
};
|
2022-02-20 20:16:49 +00:00
|
|
|
|
2022-02-19 22:55:53 +00:00
|
|
|
users = {
|
|
|
|
users."${cfg.node.sshUser}" = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = cfg.node.sshUser;
|
|
|
|
extraGroups = mkDefault [ "wheel" ];
|
|
|
|
shell = pkgs.bash;
|
|
|
|
openssh.authorizedKeys = cfg.authorizedKeys;
|
|
|
|
};
|
|
|
|
groups."${cfg.node.sshUser}" = {};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|