]> Git — Sourcephile - sourcephile-nix.git/blob - nixpkgs/patches/installer.ssh-nixos.diff
nix: use nixpkgs/patches/ instead of nixos/modules/
[sourcephile-nix.git] / nixpkgs / patches / installer.ssh-nixos.diff
1 diff --git a/nixos/modules/installer/ssh-nixos.nix b/nixos/modules/installer/ssh-nixos.nix
2 new file mode 100644
3 index 00000000000..52ac88799ee
4 --- /dev/null
5 +++ b/nixos/modules/installer/ssh-nixos.nix
6 @@ -0,0 +1,112 @@
7 +{ pkgs, lib, config, ... }:
8 +let
9 + inherit (lib) types;
10 + inherit (config) networking;
11 + cfg = config.installer.ssh-nixos;
12 + nixRunDefaultCommand = "bash";
13 + ssh = pkgs.writeShellScriptBin "ssh" ''
14 + set -eu
15 + PATH=$OLDPATH
16 + set -x
17 + ssh -l '${cfg.login}' \
18 + ${lib.escapeShellArgs cfg.sshFlags} ''${SSH_FLAGS:-} "$@"
19 + '';
20 +in
21 +{
22 +options.installer.ssh-nixos = {
23 + PATH = lib.mkOption {
24 + type = types.listOf types.package;
25 + default = [];
26 + apply = lib.makeBinPath;
27 + description = "Packages to be appended to the <literal>PATH<literal> of the script.";
28 + };
29 + script = lib.mkOption {
30 + type = types.lines;
31 + default = "";
32 + example = ''
33 + lib.mkBefore ''''''
34 + gpg --decrypt initrd/ssh.key.gpg |
35 + ssh root@''${config.installer.ssh-nixos.target} \
36 + install -D -m 400 -o root -g root /dev/stdin /root/initrd/ssh.key
37 + '''''';
38 + '';
39 + description = ''
40 + Install script copying the configured NixOS to the <link linkend="opt-install.ssh-nixos.target">target</link>
41 + and switching to the new configuration.
42 + It is made available here for prepending or appending commands
43 + with the usual <literal>mkBefore</literal> and <literal>mkAfter</literal>.
44 + In case you run it often or add multiple ssh calls to it,
45 + consider configuring the OpenSSH client with <literal>ControlMaster auto</literal>
46 + to keep the SSH connexion alive between calls to <literal>literal</literal>.
47 +
48 + This script is usually run with:
49 + <screen>
50 + <prompt>$ </prompt> nix run system.config.installer.ssh-nixos -f nixos.nix
51 + </screen>
52 + where <literal>nixos.nix</literal> can be:
53 + <screen>
54 + import <nixpkgs/nixos> {
55 + system = "x86_64-linux";
56 + configuration = { config, lib, pkgs }: {
57 + # Your usual configuration.nix content can go here
58 + };
59 + }
60 + </screen>
61 + '';
62 + apply = script: pkgs.writeShellScriptBin nixRunDefaultCommand ''
63 + set -eu
64 + set -o pipefail
65 + export OLDPATH=$PATH:${cfg.PATH}
66 + PATH="${ssh}/bin:$OLDPATH"
67 + set -x
68 + ${script}
69 + '';
70 + };
71 + login = lib.mkOption {
72 + type = types.str;
73 + default = "root";
74 + example = "admin";
75 + description = "Login name passed to ssh.";
76 + };
77 + target = lib.mkOption {
78 + type = types.str;
79 + default = "${networking.hostName}.${networking.domain}";
80 + example = "192.168.1.10";
81 + description = "Destination where to install NixOS passed to ssh.";
82 + };
83 + sshFlags = lib.mkOption {
84 + type = types.listOf types.str;
85 + default = ["-o" "ControlMaster=auto"];
86 + description = ''
87 + Extra flags passed to <literal>ssh</literal>.
88 + Environment variable <literal>SSH_FLAGS</literal> can also be used at runtime.
89 + '';
90 + };
91 + nixCopyFlags = lib.mkOption {
92 + type = types.listOf types.str;
93 + default = ["--substitute-on-destination"];
94 + description = ''
95 + Extra flags passed to <literal>nix copy</literal>.
96 + Environment variable <literal>NIX_COPY_FLAGS</literal> can also be used at runtime.
97 + '';
98 + };
99 + profile = lib.mkOption {
100 + type = types.str;
101 + default = "/nix/var/nix/profiles/system";
102 + };
103 +};
104 +config = {
105 + installer.ssh-nixos.PATH = with pkgs; [nix openssh];
106 + installer.ssh-nixos.script =
107 + let nixos = config.system.build.toplevel; in ''
108 + nix ''${NIX_FLAGS:-} copy \
109 + --to ssh://'${cfg.target}' \
110 + ${lib.escapeShellArgs cfg.nixCopyFlags} ''${NIX_COPY_FLAGS:-} \
111 + ${nixos}
112 + ssh '${cfg.target}' \
113 + nix-env --profile '${cfg.profile}' --set '${nixos}' '&&' \
114 + '${cfg.profile}'/bin/switch-to-configuration "''${NIXOS_SWITCH:-switch}"
115 + '';
116 +};
117 +meta.maintainers = [ lib.maintainers.julm ];
118 +}
119 diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
120 index f361163ca63..15659fde11b 100644
121 --- a/nixos/modules/module-list.nix
122 +++ b/nixos/modules/module-list.nix
123 @@ -80,6 +80,7 @@
124 ./i18n/input-method/ibus.nix
125 ./i18n/input-method/nabi.nix
126 ./i18n/input-method/uim.nix
127 + ./installer/ssh-nixos.nix
128 ./installer/tools/tools.nix
129 ./misc/assertions.nix
130 ./misc/crashdump.nix