aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/krops/default.nix
blob: 2a1b62982f4f08f4c4b9516d68f62e0af076909e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
let
  lib = import ../../lib;
in

{ nix, openssh, populate, writers }: rec {

  build = target:
    remoteCommand target (lib.concatStringsSep " " [
      "nix build"
      "-I ${lib.escapeShellArg target.path}"
      "--no-link -f '<nixpkgs/nixos>'"
      "config.system.build.toplevel"
    ]);

  rebuild = args: target:
    remoteCommand target "nixos-rebuild -I ${lib.escapeShellArg target.path} ${
      lib.concatMapStringsSep " " lib.escapeShellArg args
    }";

  remoteCommand = target: command:
    if lib.isLocalTarget target
      then command
      else
        writers.writeDash "krops.${target.host}.${lib.firstWord command}" ''
          exec ${openssh}/bin/ssh ${lib.escapeShellArgs (lib.flatten [
            (lib.optionals (target.user != "") ["-l" target.user])
            "-p" target.port
            "-t"
            target.extraOptions
            target.host
            (if target.sudo then "sudo ${command}" else command)])}
        '';

  writeCommand = name: {
    command ? (targetPath: "echo ${targetPath}"),
    backup ? false,
    force ? false,
    source,
    target
  }: let
    target' = lib.mkTarget target;
  in
    writers.writeDash name ''
      set -efu
      ${populate { inherit backup force source; target = target'; }}
      ${remoteCommand target' (command target'.path)}
    '';

  writeDeploy = name: {
    backup ? false,
    buildTarget ? null,
    crossDeploy ? false,
    fast ? false,
    force ? false,
    source,
    target
  }: let
    buildTarget' =
      if buildTarget == null
        then target'
        else lib.mkTarget buildTarget;
    target' = lib.mkTarget target;
  in
    writers.writeDash name ''
      set -efu
      ${lib.optionalString (buildTarget' != target')
        (populate { inherit backup force source; target = buildTarget'; })}
      ${populate { inherit backup force source; target = target'; }}
      ${lib.optionalString (! fast) ''
        ${rebuild ["dry-build"] buildTarget'}
        ${build buildTarget'}
      ''}
      ${rebuild ([
        "switch"
      ] ++ lib.optionals crossDeploy [
        "--no-build-nix"
      ] ++ lib.optionals (buildTarget' != target') [
        "--build-host" "${buildTarget'.user}@${buildTarget'.host}"
        "--target-host" "${target'.user}@${target'.host}"
      ]) buildTarget'}
    '';

  writeTest = name: {
    backup ? false,
    force ? false,
    source,
    target
  }: let
    target' = lib.mkTarget target;
  in
    assert lib.isLocalTarget target';
    writers.writeDash name ''
      set -efu
      ${populate { inherit backup force source; target = target'; }} >&2
      NIX_PATH=${lib.escapeShellArg target'.path} \
      ${nix}/bin/nix-build \
          -A system \
          --keep-going \
          --no-out-link \
          --show-trace \
          '<nixpkgs/nixos>'
    '';
}