blob: 10b5d4fce7675200b9133f3e2da8efee076273e2 [file] [log] [blame]
Serge Bazanski6abe4fa2020-10-03 00:18:34 +02001# Top-level file aggregating all machines managed from hscloud.
2#
3# This allows to have a common attrset of machines that can be deployed
4# in the same way.
5#
Serge Bazanskib3c67702021-09-10 22:27:24 +00006# For information about building/deploying machines see //ops/README.md.
Serge Bazanski6abe4fa2020-10-03 00:18:34 +02007
Serge Bazanskic35ea6a2022-07-07 17:47:58 +02008{ hscloud, pkgs, hscloudForPkgs, ... }:
Serge Bazanski6abe4fa2020-10-03 00:18:34 +02009
10let
Serge Bazanskib3c67702021-09-10 22:27:24 +000011 # nixpkgs for cluster machines (.hswaw.net). Currently pinned to an old
12 # nixpkgs because NixOS modules for kubernetes changed enough that it's not
13 # super easy to use them as is.
14 #
15 # TODO(q3k): fix this: use an old nixpkgs for Kube modules while using
16 # hscloud nixpkgs for everything else.
17 nixpkgsCluster = import (pkgs.fetchFromGitHub {
18 owner = "nixos";
19 repo = "nixpkgs-channels";
20 rev = "44ad80ab1036c5cc83ada4bfa451dac9939f2a10";
21 sha256 = "1b61nzvy0d46cspy07szkc0rggacxiqg9v1py27pkqpj7rvawfsk";
Serge Bazanski3a9562e2023-02-28 01:14:26 +000022 }) { };
Serge Bazanskib3c67702021-09-10 22:27:24 +000023
Serge Bazanskia16af2d2021-10-16 19:14:05 +000024 # mkMachine builds NixOS modules into a NixOS derivation.
25 # It:
26 # 1) injects passthru.hscloud.provision which deploys that configuration
27 # over SSH to a production machine.
28 # 2) injects 'workspace' as a nixos module argument which points to the root
29 # of the hscloud readTree object. It will contain whatever nixpkgs
30 # checkout this file has been invoked with, ie. will not be 'mixed in'
31 # with the pkgs argument.
Serge Bazanski55a486a2022-06-11 18:27:01 +000032 mkMachine = machines: pkgs: paths: pkgs.nixos ({ config, pkgs, ... }: {
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020033 imports = paths;
Serge Bazanskib3c67702021-09-10 22:27:24 +000034
35 config = let
36 name = config.networking.hostName;
37 domain = if (config.networking ? domain) && config.networking.domain != null then config.networking.domain else "hswaw.net";
38 fqdn = name + "." + domain;
39 toplevel = config.system.build.toplevel;
40
41 runProvision = ''
42 #!/bin/sh
43 set -eu
44 remote=root@${fqdn}
45 echo "Configuration for ${fqdn} is ${toplevel}"
46 nix copy -s --to ssh://$remote ${toplevel}
47
48 running="$(ssh $remote readlink -f /nix/var/nix/profiles/system)"
49 if [ "$running" == "${toplevel}" ]; then
50 echo "${fqdn} already running ${toplevel}."
51 else
52 echo "/etc/systemd/system diff:"
53 ssh $remote diff -ur /var/run/current-system/etc/systemd/system ${toplevel}/etc/systemd/system || true
54 echo ""
55 echo ""
56 echo "dry-activate diff:"
57 ssh $remote ${toplevel}/bin/switch-to-configuration dry-activate
58 read -p "Do you want to switch to this configuration? " -n 1 -r
59 echo
60 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
61 exit 1
62 fi
63
64 echo -ne "\n\nswitch-to-configuration test...\n"
65 ssh $remote ${toplevel}/bin/switch-to-configuration test
66 fi
67
68 echo -ne "\n\n"
69 read -p "Do you want to set this configuration as boot? " -n 1 -r
70 echo
71 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
72 exit 1
73 fi
74
75 echo -ne "\n\nsetting system profile...\n"
76 ssh $remote nix-env -p /nix/var/nix/profiles/system --set ${toplevel}
77
78 echo -ne "\n\nswitch-to-configuration boot...\n"
79 ssh $remote ${toplevel}/bin/switch-to-configuration boot
80 '';
81 in {
82 passthru.hscloud.provision = pkgs.writeScript "provision-${fqdn}" runProvision;
Serge Bazanskia16af2d2021-10-16 19:14:05 +000083
84 # TODO(q3k): this should be named hscloud, but that seems to not work. Debug and rename.
Serge Bazanskic35ea6a2022-07-07 17:47:58 +020085 _module.args.workspace = hscloudForPkgs pkgs;
Serge Bazanski55a486a2022-06-11 18:27:01 +000086 _module.args.machines = machines;
Serge Bazanskib3c67702021-09-10 22:27:24 +000087 };
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020088 });
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020089
Serge Bazanski55a486a2022-06-11 18:27:01 +000090 mkClusterMachine = machines: path: mkMachine machines nixpkgsCluster [
91 ../cluster/machines/modules/base.nix
92 ../cluster/machines/modules/kube-controlplane.nix
93 ../cluster/machines/modules/kube-dataplane.nix
Serge Bazanski55a486a2022-06-11 18:27:01 +000094 path
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020095 ];
Piotr Dobrowolskia01905a2021-10-16 18:22:46 +020096
Serge Bazanskief3aab62022-11-18 14:39:45 +000097 mkClusterMachineNew = machines: path: mkMachine machines pkgs [
98 ../cluster/machines/modules/base.nix
99 ../cluster/machines/modules/kube-controlplane.nix
100 ../cluster/machines/modules/kube-dataplane.nix
Serge Bazanskief3aab62022-11-18 14:39:45 +0000101 path
102 ];
103
104
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200105 pkgsArm = import pkgs.path {
106 system = "aarch64-linux";
107 };
108
Serge Bazanski55a486a2022-06-11 18:27:01 +0000109 machines = self: {
Serge Bazanskief3aab62022-11-18 14:39:45 +0000110 "bc01n01.hswaw.net" = mkClusterMachineNew self ../cluster/machines/bc01n01.hswaw.net.nix;
Serge Bazanski55a486a2022-06-11 18:27:01 +0000111 "bc01n02.hswaw.net" = mkClusterMachine self ../cluster/machines/bc01n02.hswaw.net.nix;
112 "dcr01s22.hswaw.net" = mkClusterMachine self ../cluster/machines/dcr01s22.hswaw.net.nix;
113 "dcr01s24.hswaw.net" = mkClusterMachine self ../cluster/machines/dcr01s24.hswaw.net.nix;
114
Serge Bazanski957d9112022-06-12 12:26:02 +0200115 "edge01.waw.bgp.wtf" = mkMachine self pkgs [
Serge Bazanski55a486a2022-06-11 18:27:01 +0000116 ../bgpwtf/machines/edge01.waw.bgp.wtf.nix
117 ../bgpwtf/machines/edge01.waw.bgp.wtf-hardware.nix
118 ];
119
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200120 "larrythebuilder.q3k.org" = mkMachine self pkgsArm [
121 ../hswaw/machines/larrythebuilder.q3k.org/configuration.nix
122 ];
123
Serge Bazanski55a486a2022-06-11 18:27:01 +0000124 "customs.hackerspace.pl" = mkMachine self pkgs [
125 ../hswaw/machines/customs.hackerspace.pl/configuration.nix
126 ];
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200127 "tv1.waw.hackerspace.pl" = mkMachine self pkgsArm [
Serge Bazanskidcdbd842022-07-07 02:30:09 +0200128 ../hswaw/machines/tv/tv1.nix
129 ];
130 "tv2.waw.hackerspace.pl" = mkMachine self pkgsArm [
131 ../hswaw/machines/tv/tv2.nix
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200132 ];
vukodeeeff82022-10-02 23:12:29 +0200133 "sound.waw.hackerspace.pl" = let
134 # TODO update global pkgs to >= 22.05 and remove this override
135 # building on current pkgs gives error:
136 # error: The option `services.home-assistant.extraComponents' does not exist.
137 pkgs = import (fetchTarball {
138 # NixOS/nixpkgs/nixos-unstable 2022-09-10
139 url = "https://api.github.com/repos/NixOS/nixpkgs/tarball/2da64a81275b68fdad38af669afeda43d401e94b";
140 sha256 = "1k71lmzdaa48yqkmsnd22n177qmxxi4gj2qcmdbv0mc6l4f27wd0";
141 }) {};
142 in mkMachine self pkgs [
143 ../hswaw/machines/sound.waw.hackerspace.pl/configuration.nix
144 ];
Serge Bazanski55a486a2022-06-11 18:27:01 +0000145 };
146
147in pkgs.lib.fix machines