From a73714a93c2d5e1ce2f9e52f23eaf3019a5d46ed Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Fri, 3 Jul 2020 00:26:33 -0400 Subject: feat(ops/nixos): Add generic rebuild-system script This adds a first crack at one idea for a generic, non-user-specific rebuild-system script to ops.nixos.rebuild-system. The idea here is that we enumerate all the nixos systems stored in the monorepo (similarly to what we do for ci-builds right now) then search through them by hostname to find the one matching the hostname of the current system, which is an attempt at a more generic version of tazjin's rebuilder script which does the same thing but with an explicit case block. As a caveat, it feels like there's a slight possibility that this way of finding systems is going to get slow to evaluate - on my system it feels fine but if it grows out of hand it's probably feasible to just bake this into the built script as a dynamically generated case statement. Change-Id: I2e4c5401913b6f4d936ab48ba2f95f96e0e78eb4 Reviewed-on: https://cl.tvl.fyi/c/depot/+/894 Tested-by: BuildkiteCI Reviewed-by: lukegb --- ops/nixos/all-systems.nix | 14 ++++++++++++++ ops/nixos/default.nix | 36 ++++++++++++++++++++++++++++++++++-- ops/nixos/whitby/default.nix | 6 +----- 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 ops/nixos/all-systems.nix (limited to 'ops/nixos') diff --git a/ops/nixos/all-systems.nix b/ops/nixos/all-systems.nix new file mode 100644 index 000000000000..ba63de2f2dd5 --- /dev/null +++ b/ops/nixos/all-systems.nix @@ -0,0 +1,14 @@ +{ depot, ... }: + +(with depot.ops.nixos; [ + whitby +]) ++ + +(with depot.users.tazjin.nixos; [ + camden + frog +]) ++ + +(with depot.users.glittershark.system.system; [ + chupacabra +]) diff --git a/ops/nixos/default.nix b/ops/nixos/default.nix index a0d7630d00e0..40431a79afaf 100644 --- a/ops/nixos/default.nix +++ b/ops/nixos/default.nix @@ -6,8 +6,40 @@ # # TODO(tazjin): Find a more elegant solution for the whole module # situation. -{ ... }@args: +{ lib, pkgs, depot, ... }@args: -{ +let + inherit (lib) findFirst isAttrs; +in + +rec { whitby = import ./whitby/default.nix args; + + # System installation + + allSystems = import ./all-systems.nix args; + + nixosFor = configuration: depot.third_party.nixos { + inherit configuration; + }; + + findSystem = hostname: + (findFirst + (system: system.config.networking.hostName == hostname) + (throw "${hostname} is not a known NixOS host") + (map nixosFor allSystems)); + + rebuild-system = pkgs.writeShellScriptBin "rebuild-system" '' + set -ue + if [[ $EUID -ne 0 ]]; then + echo "Oh no! Only root is allowed to rebuild the system!" >&2 + exit 1 + fi + + echo "Rebuilding NixOS for $HOSTNAME" + system=$(nix-build -E "((import ${toString depot.depotPath} {}).ops.nixos.findSystem \"$HOSTNAME\").system" --no-out-link --show-trace) + + nix-env -p /nix/var/nix/profiles/system --set $system + $system/bin/switch-to-configuration switch + ''; } diff --git a/ops/nixos/whitby/default.nix b/ops/nixos/whitby/default.nix index 73066d6b9cad..b1055ddd2ee1 100644 --- a/ops/nixos/whitby/default.nix +++ b/ops/nixos/whitby/default.nix @@ -12,11 +12,7 @@ let mkdir -p $out/bin ln -s ${depot.ops.besadii}/bin/besadii $out/bin/post-command ''; - - systemForConfig = configuration: (depot.third_party.nixos { - inherit configuration; - }).system; -in systemForConfig { +in { inherit depot; imports = [ "${depot.depotPath}/ops/nixos/depot.nix" -- cgit 1.4.1