blob: 710762e337f6582be561677ec1de98baad9ca866 [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 Bazanski9848e7e2021-09-10 22:30:56 +000022 }) {
23 overlays = [
24 (self: super: rec {
25 # Use a newer version of Ceph (16, Pacific, EOL 2023-06-01) than in
26 # this nixpkgs (15, Octopus, EOL 2022-06-01).
27 #
28 # This is to:
29 # 1. Fix a bug in which ceph-volume lvm create fails due to a rocksdb
30 # mismatch (https://tracker.ceph.com/issues/49815)
31 # 2. At the time of deployment not start out with an ancient version
32 # of Ceph.
33 #
34 # Once we unpin nixpkgsCluster past a version that contains this Ceph,
35 # this can be unoverlayed.
36 inherit (super.callPackages ./ceph {
37 boost = super.boost17x.override { enablePython = true; python = super.python3; };
38 lua = super.lua5_4;
39 }) ceph ceph-client;
40 ceph-lib = ceph.lib;
41 })
42 ];
43 };
Serge Bazanskib3c67702021-09-10 22:27:24 +000044
Serge Bazanskia16af2d2021-10-16 19:14:05 +000045 # mkMachine builds NixOS modules into a NixOS derivation.
46 # It:
47 # 1) injects passthru.hscloud.provision which deploys that configuration
48 # over SSH to a production machine.
49 # 2) injects 'workspace' as a nixos module argument which points to the root
50 # of the hscloud readTree object. It will contain whatever nixpkgs
51 # checkout this file has been invoked with, ie. will not be 'mixed in'
52 # with the pkgs argument.
Serge Bazanski55a486a2022-06-11 18:27:01 +000053 mkMachine = machines: pkgs: paths: pkgs.nixos ({ config, pkgs, ... }: {
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020054 imports = paths;
Serge Bazanskib3c67702021-09-10 22:27:24 +000055
56 config = let
57 name = config.networking.hostName;
58 domain = if (config.networking ? domain) && config.networking.domain != null then config.networking.domain else "hswaw.net";
59 fqdn = name + "." + domain;
60 toplevel = config.system.build.toplevel;
61
62 runProvision = ''
63 #!/bin/sh
64 set -eu
65 remote=root@${fqdn}
66 echo "Configuration for ${fqdn} is ${toplevel}"
67 nix copy -s --to ssh://$remote ${toplevel}
68
69 running="$(ssh $remote readlink -f /nix/var/nix/profiles/system)"
70 if [ "$running" == "${toplevel}" ]; then
71 echo "${fqdn} already running ${toplevel}."
72 else
73 echo "/etc/systemd/system diff:"
74 ssh $remote diff -ur /var/run/current-system/etc/systemd/system ${toplevel}/etc/systemd/system || true
75 echo ""
76 echo ""
77 echo "dry-activate diff:"
78 ssh $remote ${toplevel}/bin/switch-to-configuration dry-activate
79 read -p "Do you want to switch to this configuration? " -n 1 -r
80 echo
81 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
82 exit 1
83 fi
84
85 echo -ne "\n\nswitch-to-configuration test...\n"
86 ssh $remote ${toplevel}/bin/switch-to-configuration test
87 fi
88
89 echo -ne "\n\n"
90 read -p "Do you want to set this configuration as boot? " -n 1 -r
91 echo
92 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
93 exit 1
94 fi
95
96 echo -ne "\n\nsetting system profile...\n"
97 ssh $remote nix-env -p /nix/var/nix/profiles/system --set ${toplevel}
98
99 echo -ne "\n\nswitch-to-configuration boot...\n"
100 ssh $remote ${toplevel}/bin/switch-to-configuration boot
101 '';
102 in {
103 passthru.hscloud.provision = pkgs.writeScript "provision-${fqdn}" runProvision;
Serge Bazanskia16af2d2021-10-16 19:14:05 +0000104
105 # TODO(q3k): this should be named hscloud, but that seems to not work. Debug and rename.
Serge Bazanskic35ea6a2022-07-07 17:47:58 +0200106 _module.args.workspace = hscloudForPkgs pkgs;
Serge Bazanski55a486a2022-06-11 18:27:01 +0000107 _module.args.machines = machines;
Serge Bazanskib3c67702021-09-10 22:27:24 +0000108 };
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200109 });
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200110
Serge Bazanski55a486a2022-06-11 18:27:01 +0000111 mkClusterMachine = machines: path: mkMachine machines nixpkgsCluster [
112 ../cluster/machines/modules/base.nix
113 ../cluster/machines/modules/kube-controlplane.nix
114 ../cluster/machines/modules/kube-dataplane.nix
115 ../cluster/machines/modules/ceph.nix
116 path
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200117 ];
Piotr Dobrowolskia01905a2021-10-16 18:22:46 +0200118
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200119 pkgsArm = import pkgs.path {
120 system = "aarch64-linux";
121 };
122
Serge Bazanski55a486a2022-06-11 18:27:01 +0000123 machines = self: {
124 "bc01n01.hswaw.net" = mkClusterMachine self ../cluster/machines/bc01n01.hswaw.net.nix;
125 "bc01n02.hswaw.net" = mkClusterMachine self ../cluster/machines/bc01n02.hswaw.net.nix;
126 "dcr01s22.hswaw.net" = mkClusterMachine self ../cluster/machines/dcr01s22.hswaw.net.nix;
127 "dcr01s24.hswaw.net" = mkClusterMachine self ../cluster/machines/dcr01s24.hswaw.net.nix;
128
Serge Bazanski957d9112022-06-12 12:26:02 +0200129 "edge01.waw.bgp.wtf" = mkMachine self pkgs [
Serge Bazanski55a486a2022-06-11 18:27:01 +0000130 ../bgpwtf/machines/edge01.waw.bgp.wtf.nix
131 ../bgpwtf/machines/edge01.waw.bgp.wtf-hardware.nix
132 ];
133
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200134 "larrythebuilder.q3k.org" = mkMachine self pkgsArm [
135 ../hswaw/machines/larrythebuilder.q3k.org/configuration.nix
136 ];
137
Serge Bazanski55a486a2022-06-11 18:27:01 +0000138 "customs.hackerspace.pl" = mkMachine self pkgs [
139 ../hswaw/machines/customs.hackerspace.pl/configuration.nix
140 ];
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200141 "tv1.waw.hackerspace.pl" = mkMachine self pkgsArm [
Serge Bazanskidcdbd842022-07-07 02:30:09 +0200142 ../hswaw/machines/tv/tv1.nix
143 ];
144 "tv2.waw.hackerspace.pl" = mkMachine self pkgsArm [
145 ../hswaw/machines/tv/tv2.nix
Serge Bazanski5ac5e4b2022-07-06 00:31:35 +0200146 ];
vukodeeeff82022-10-02 23:12:29 +0200147 "sound.waw.hackerspace.pl" = let
148 # TODO update global pkgs to >= 22.05 and remove this override
149 # building on current pkgs gives error:
150 # error: The option `services.home-assistant.extraComponents' does not exist.
151 pkgs = import (fetchTarball {
152 # NixOS/nixpkgs/nixos-unstable 2022-09-10
153 url = "https://api.github.com/repos/NixOS/nixpkgs/tarball/2da64a81275b68fdad38af669afeda43d401e94b";
154 sha256 = "1k71lmzdaa48yqkmsnd22n177qmxxi4gj2qcmdbv0mc6l4f27wd0";
155 }) {};
156 in mkMachine self pkgs [
157 ../hswaw/machines/sound.waw.hackerspace.pl/configuration.nix
158 ];
Serge Bazanski55a486a2022-06-11 18:27:01 +0000159 };
160
161in pkgs.lib.fix machines