blob: 51a1f0df85bc183c38f1d094bed4980ff86e0cd2 [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 Bazanski8f084232023-03-10 20:52:06 +010024 # TODO(patryk): unpin and upgrade
25 nixpkgsBgpwtf = import (pkgs.fetchFromGitHub {
26 owner = "nixos";
27 repo = "nixpkgs-channels";
28 rev = "e26c0ffdb013cd378fc2528a44689a8bf35d2a6c";
29 sha256 = "1b33hw35fqb9rzszdg5jpiyfvhx2cxpv0qrkyr19zkdpdahzdbss";
30 }) { };
31
32
Serge Bazanskia16af2d2021-10-16 19:14:05 +000033 # mkMachine builds NixOS modules into a NixOS derivation.
34 # It:
35 # 1) injects passthru.hscloud.provision which deploys that configuration
36 # over SSH to a production machine.
37 # 2) injects 'workspace' as a nixos module argument which points to the root
38 # of the hscloud readTree object. It will contain whatever nixpkgs
39 # checkout this file has been invoked with, ie. will not be 'mixed in'
40 # with the pkgs argument.
Serge Bazanski55a486a2022-06-11 18:27:01 +000041 mkMachine = machines: pkgs: paths: pkgs.nixos ({ config, pkgs, ... }: {
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020042 imports = paths;
Serge Bazanskib3c67702021-09-10 22:27:24 +000043
44 config = let
45 name = config.networking.hostName;
46 domain = if (config.networking ? domain) && config.networking.domain != null then config.networking.domain else "hswaw.net";
47 fqdn = name + "." + domain;
48 toplevel = config.system.build.toplevel;
49
50 runProvision = ''
51 #!/bin/sh
52 set -eu
53 remote=root@${fqdn}
54 echo "Configuration for ${fqdn} is ${toplevel}"
55 nix copy -s --to ssh://$remote ${toplevel}
56
57 running="$(ssh $remote readlink -f /nix/var/nix/profiles/system)"
58 if [ "$running" == "${toplevel}" ]; then
59 echo "${fqdn} already running ${toplevel}."
60 else
61 echo "/etc/systemd/system diff:"
62 ssh $remote diff -ur /var/run/current-system/etc/systemd/system ${toplevel}/etc/systemd/system || true
63 echo ""
64 echo ""
65 echo "dry-activate diff:"
66 ssh $remote ${toplevel}/bin/switch-to-configuration dry-activate
67 read -p "Do you want to switch to this configuration? " -n 1 -r
68 echo
69 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
70 exit 1
71 fi
72
73 echo -ne "\n\nswitch-to-configuration test...\n"
74 ssh $remote ${toplevel}/bin/switch-to-configuration test
75 fi
76
77 echo -ne "\n\n"
78 read -p "Do you want to set this configuration as boot? " -n 1 -r
79 echo
80 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
81 exit 1
82 fi
83
84 echo -ne "\n\nsetting system profile...\n"
85 ssh $remote nix-env -p /nix/var/nix/profiles/system --set ${toplevel}
86
87 echo -ne "\n\nswitch-to-configuration boot...\n"
88 ssh $remote ${toplevel}/bin/switch-to-configuration boot
89 '';
90 in {
91 passthru.hscloud.provision = pkgs.writeScript "provision-${fqdn}" runProvision;
Serge Bazanskia16af2d2021-10-16 19:14:05 +000092
93 # TODO(q3k): this should be named hscloud, but that seems to not work. Debug and rename.
Serge Bazanskic35ea6a2022-07-07 17:47:58 +020094 _module.args.workspace = hscloudForPkgs pkgs;
Serge Bazanski55a486a2022-06-11 18:27:01 +000095 _module.args.machines = machines;
Serge Bazanskib3c67702021-09-10 22:27:24 +000096 };
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020097 });
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020098
Serge Bazanski55a486a2022-06-11 18:27:01 +000099 mkClusterMachine = machines: path: mkMachine machines nixpkgsCluster [
100 ../cluster/machines/modules/base.nix
101 ../cluster/machines/modules/kube-controlplane.nix
102 ../cluster/machines/modules/kube-dataplane.nix
Serge Bazanski55a486a2022-06-11 18:27:01 +0000103 path
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200104 ];
Piotr Dobrowolskia01905a2021-10-16 18:22:46 +0200105
Serge Bazanskif6e6abb2023-03-31 22:39:45 +0000106 mkClusterMachineNew = machines: path: mkMachine machines nixpkgsBgpwtf [
Serge Bazanskief3aab62022-11-18 14:39:45 +0000107 ../cluster/machines/modules/base.nix
108 ../cluster/machines/modules/kube-controlplane.nix
109 ../cluster/machines/modules/kube-dataplane.nix
Serge Bazanskief3aab62022-11-18 14:39:45 +0000110 path
111 ];
112
113
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200114 pkgsArm = import pkgs.path {
115 system = "aarch64-linux";
116 };
117
Serge Bazanski55a486a2022-06-11 18:27:01 +0000118 machines = self: {
Serge Bazanskief3aab62022-11-18 14:39:45 +0000119 "bc01n01.hswaw.net" = mkClusterMachineNew self ../cluster/machines/bc01n01.hswaw.net.nix;
Serge Bazanski55a486a2022-06-11 18:27:01 +0000120 "bc01n02.hswaw.net" = mkClusterMachine self ../cluster/machines/bc01n02.hswaw.net.nix;
Serge Bazanski712a5dc2023-02-28 01:15:40 +0000121 "bc01n05.hswaw.net" = mkClusterMachineNew self ../cluster/machines/bc01n05.hswaw.net.nix;
Serge Bazanski55a486a2022-06-11 18:27:01 +0000122 "dcr01s22.hswaw.net" = mkClusterMachine self ../cluster/machines/dcr01s22.hswaw.net.nix;
123 "dcr01s24.hswaw.net" = mkClusterMachine self ../cluster/machines/dcr01s24.hswaw.net.nix;
124
Serge Bazanski8f084232023-03-10 20:52:06 +0100125 "edge01.waw.bgp.wtf" = mkMachine self nixpkgsBgpwtf [
Serge Bazanski55a486a2022-06-11 18:27:01 +0000126 ../bgpwtf/machines/edge01.waw.bgp.wtf.nix
127 ../bgpwtf/machines/edge01.waw.bgp.wtf-hardware.nix
128 ];
129
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200130 "larrythebuilder.q3k.org" = mkMachine self pkgsArm [
131 ../hswaw/machines/larrythebuilder.q3k.org/configuration.nix
132 ];
133
Serge Bazanski55a486a2022-06-11 18:27:01 +0000134 "customs.hackerspace.pl" = mkMachine self pkgs [
135 ../hswaw/machines/customs.hackerspace.pl/configuration.nix
136 ];
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200137 "tv1.waw.hackerspace.pl" = mkMachine self pkgsArm [
Serge Bazanskidcdbd842022-07-07 02:30:09 +0200138 ../hswaw/machines/tv/tv1.nix
139 ];
140 "tv2.waw.hackerspace.pl" = mkMachine self pkgsArm [
141 ../hswaw/machines/tv/tv2.nix
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200142 ];
vukodeeeff82022-10-02 23:12:29 +0200143 "sound.waw.hackerspace.pl" = let
144 # TODO update global pkgs to >= 22.05 and remove this override
145 # building on current pkgs gives error:
146 # error: The option `services.home-assistant.extraComponents' does not exist.
147 pkgs = import (fetchTarball {
148 # NixOS/nixpkgs/nixos-unstable 2022-09-10
149 url = "https://api.github.com/repos/NixOS/nixpkgs/tarball/2da64a81275b68fdad38af669afeda43d401e94b";
150 sha256 = "1k71lmzdaa48yqkmsnd22n177qmxxi4gj2qcmdbv0mc6l4f27wd0";
151 }) {};
152 in mkMachine self pkgs [
153 ../hswaw/machines/sound.waw.hackerspace.pl/configuration.nix
154 ];
Serge Bazanski55a486a2022-06-11 18:27:01 +0000155 };
156
157in pkgs.lib.fix machines