default.nix 1.25 KB
Newer Older
Matthias Adamczyk's avatar
Matthias Adamczyk committed
1
2
3
4
5
{ config, pkgs, lib, ... }:

with lib;

let
Matthias Adamczyk's avatar
Fix CI    
Matthias Adamczyk committed
6
  cfg = config.finf.deploy;
Matthias Adamczyk's avatar
Matthias Adamczyk committed
7
8
9

in {
  options = {
Matthias Adamczyk's avatar
Fix CI    
Matthias Adamczyk committed
10
    finf.deploy = {
Matthias Adamczyk's avatar
Matthias Adamczyk committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
      enable = mkOption {
        type = types.bool;
        default = true;
      };

      ssh.host = mkOption {
        type = types.str;
        default = "${config.networking.hostName}.${config.networking.domain}";
      };

      ssh.port = mkOption {
        type = types.int;
        default = head config.services.openssh.ports;
      };

      groups = mkOption {
        type = with types; listOf str;
        default = [];
      };
    };
  };

  config = mkIf cfg.enable {
Matthias Adamczyk's avatar
Fix CI    
Matthias Adamczyk committed
34
    finf.deploy.groups = [ "all" config.nixpkgs.system ];
Matthias Adamczyk's avatar
Matthias Adamczyk committed
35
36
37
38
39
40
41
42
43
44
45
46

    system.build.deployScript = pkgs.writeScript "deploy-${config.networking.hostName}" ''
      #!${pkgs.runtimeShell}
      set -xeo pipefail
      export PATH=${with pkgs; lib.makeBinPath [ coreutils openssh nix ]}
      export NIX_SSHOPTS="$NIX_SSHOPTS -p${toString cfg.ssh.port}"
      nix copy --no-check-sigs --to ssh://${cfg.ssh.host} ${config.system.build.toplevel}
      ssh $NIX_SSHOPTS ${cfg.ssh.host} "sudo nix-env -p /nix/var/nix/profiles/system -i ${config.system.build.toplevel}" 
      ssh $NIX_SSHOPTS ${cfg.ssh.host} "sudo /nix/var/nix/profiles/system/bin/switch-to-configuration $1" 
    '';
  };
}