tvl-depot/ops/nixos.nix
sterni e2807ec934 fix(ops/nixos): use builtins.storePath to avoid dumping pkgs.path
This is a less invasive way to achieve the same goal as cl/5681, by
preventing the already existing nixpkgs store path from being dumped
again at the call site. To support nixpkgsBisectPath, we simply check if
pkgs.path is below builtins.storeDir and use builtins.storePath based on
that.

This is actually similar to the approach taken in the nixpkgs
documentation system which tries to limit the amount of nixpkgs that
needs to be dumped by using filterSource on specific subtrees of
nixpkgs. For this to work it has to insist on pkgs.path being an
ordinary Nix path, though.

Change-Id: Idf892f90a5d811184568e4702a901c334d56210e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5787
Autosubmit: sterni <sternenseemann@systemli.org>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
2022-05-29 10:22:25 +00:00

63 lines
2 KiB
Nix

# Helper functions for instantiating depot-compatible NixOS machines.
{ depot, lib, pkgs, ... }@args:
let inherit (lib) findFirst isAttrs;
in rec {
# This provides our standard set of arguments to all NixOS modules.
baseModule = { ... }: {
# Ensure that pkgs == third_party.nix
nixpkgs.pkgs = depot.third_party.nixpkgs;
nix.nixPath =
let
# Due to nixpkgsBisectPath, pkgs.path is not always in the nix store
nixpkgsStorePath =
if lib.hasPrefix builtins.storeDir (toString pkgs.path)
then builtins.storePath pkgs.path # nixpkgs is already in the store
else pkgs.path; # we need to dump nixpkgs to the store either way
in
[
("nixos=" + nixpkgsStorePath)
("nixpkgs=" + nixpkgsStorePath)
];
};
nixosFor = configuration: (depot.third_party.nixos {
configuration = { ... }: {
imports = [
baseModule
configuration
];
};
specialArgs = {
inherit (args) depot;
};
});
findSystem = hostname:
(findFirst
(system: system.config.networking.hostName == hostname)
(throw "${hostname} is not a known NixOS host")
(map nixosFor depot.ops.machines.all-systems));
rebuild-system = rebuildSystemWith depot.path;
rebuildSystemWith = depotPath: 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=$(${pkgs.nix}/bin/nix-build -E "((import ${depotPath} {}).ops.nixos.findSystem \"$HOSTNAME\").system" --no-out-link --show-trace)
${pkgs.nix}/bin/nix-env -p /nix/var/nix/profiles/system --set $system
$system/bin/switch-to-configuration switch
'';
# Systems that should be built in CI
whitbySystem = (nixosFor depot.ops.machines.whitby).system;
sandunySystem = (nixosFor depot.ops.machines.sanduny).system;
meta.ci.targets = [ "sandunySystem" "whitbySystem" ];
}