From 29fe1fbeca944fbeda9711493c146caa416874b1 Mon Sep 17 00:00:00 2001 From: yuri Date: Sun, 30 Jul 2023 01:38:31 +0200 Subject: [PATCH] Test host specific nixpkgs --- flake.lock | 25 +++++++++++++--- flake.nix | 85 ++++++++++-------------------------------------------- helper.nix | 45 +++++++++++++++-------------- hosts.nix | 47 ++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 96 deletions(-) create mode 100644 hosts.nix diff --git a/flake.lock b/flake.lock index ad39d0d..5034288 100644 --- a/flake.lock +++ b/flake.lock @@ -38,11 +38,27 @@ }, "nixpkgs": { "locked": { - "lastModified": 1690538549, - "narHash": "sha256-FfScFHxidupVGPw9BrQOHz/SoFLRjoNmVC5ymS+g8xU=", + "lastModified": 1690630041, + "narHash": "sha256-gbnvqm5goS9DSKAqGFpq3398aOpwejmq4qWikqmQyRo=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "de5ca86149b0c4ff8bf69782cd25896fff0254e1", + "rev": "d57e8c535d4cbb07f441c30988ce52eec69db7a8", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.05-small", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-unstable": { + "locked": { + "lastModified": 1690628621, + "narHash": "sha256-fHmW03fQziNt1+tt/Goa0lwObsR8kY8auNEWnv92Sfw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "9508c7104d697af158ebc719586d64eb7b64c0d7", "type": "github" }, "original": { @@ -55,7 +71,8 @@ "root": { "inputs": { "nixos-generators": "nixos-generators", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "nixpkgs-unstable": "nixpkgs-unstable" } } }, diff --git a/flake.nix b/flake.nix index c8ae23e..92fda46 100644 --- a/flake.nix +++ b/flake.nix @@ -1,90 +1,35 @@ { inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05-small"; + nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable-small"; nixos-generators = { url = "github:nix-community/nixos-generators"; inputs.nixpkgs.follows = "nixpkgs"; }; }; - outputs = { self, nixpkgs, nixos-generators, ... }@inputs: let + outputs = { self, nixpkgs, nixpkgs-unstable, nixos-generators, ... }@inputs: let + hosts = import ./hosts.nix inputs; helper = (import ./helper.nix) inputs; in { - hosts = { - hydra = { - site = "vs"; - }; - iperf = { - site = "vs"; - }; - jackett = { - site = "vs"; - }; - nitter = { - site = "vs"; - }; - nixos-coturn = { - site = "vs"; - }; - tor-relay = { - site = "vs"; - }; - web-public-2 = { - site = "vs"; - }; - }; - - generateColmenaHost = name: host: { - deployment = { - targetHost = "${name}.${host.site}.grzb.de"; - targetUser = "colmena-deploy"; - }; - - imports = [ - ./configuration/common - ./configuration/proxmox-vm - ./configuration/proxmox-vm/hardware-configuration.nix - ./hosts/${name} - ] ++ nixpkgs.lib.optional (builtins.pathExists ./hosts/${name}/secrets.nix) ./hosts/${name}/secrets.nix; - }; - colmena = { meta = { - nixpkgs = import nixpkgs { - system = "x86_64-linux"; - }; + # Set the default pkgs, which is pointless in this case, + # because nodeNixpkgs is overriding it anyway and a default value is generated. + # It is still needed for colmena to run. + nixpkgs = nixpkgs.legacyPackages."x86_64-linux"; + + # Specify nixpkgs to use for each host. + # The default is "nixpkgs" for "x86_64-linux" systems, + # but it is overridden by the host-specific "hostNixpkgs" and "system" attributes. + nodeNixpkgs = builtins.mapAttrs (name: host: host.pkgs) hosts; }; - } // builtins.mapAttrs (self.generateColmenaHost) self.hosts; - - nixosConfigurations = nixpkgs.lib.mapAttrs (name: config: let - nodeNixpkgs = self.outputs.colmena.meta.nodeNixpkgs.${name} or self.outputs.colmena.meta.nixpkgs; - nodeNixos = import (nodeNixpkgs.path + "/nixos/lib/eval-config.nix"); - in nodeNixos { - modules = [ - #self.outputs.colmena.defaults - config - #inputs.colmena.nixosModules.deploymentOptions - { - _module.args.name = nixpkgs.lib.mkForce name; - _module.args.nodes = self.outputs.nixosConfigurations; - #nixpkgs.overlays = nixpkgs.lib.attrValues self.overlays; - } - ]; - inherit (nodeNixpkgs) system; - } - ) (builtins.removeAttrs self.outputs.colmena ["meta" "defaults"]); + } // builtins.mapAttrs (helper.generateColmenaHost) hosts; hydraJobs = { - nixosConfigurations = nixpkgs.lib.mapAttrs (_: config: config.config.system.build.toplevel) self.outputs.nixosConfigurations; + nixConfigurations = builtins.mapAttrs (helper.generateNixConfiguration) hosts; }; - /* - nixosConfigurations = (builtins.mapAttrs (helper.mapToNixosConfigurations) self.hosts); - hydraJobs = { - nixConfigurations = helper.buildHosts self.nixosConfigurations; - }; - */ - # Generate a base VM image for Proxmox with `nix build .#base-proxmox` packages.x86_64-linux = { base-proxmox = nixos-generators.nixosGenerate { diff --git a/helper.nix b/helper.nix index 07a3e8e..fc91e4e 100644 --- a/helper.nix +++ b/helper.nix @@ -1,25 +1,26 @@ -{ nixpkgs, ... }@inputs: -rec { - generateNixosSystem = name: { - system ? "x86_64-linux", - group ? null, - modules ? [], - site - }: let - localNixpkgs = nixpkgs.lib.attrByPath [ "nixpkgs-${name}" ] nixpkgs inputs; - in localNixpkgs.lib.nixosSystem { - system = system; - modules = modules ++ [ - ./configuration/common - ./configuration/proxmox-vm - ./configuration/proxmox-vm/hardware-configuration.nix - ./hosts/${name} - ]; +{ nixpkgs, ... }: +{ + generateColmenaHost = name: { + site, + modules, + ... + }: { + deployment = { + targetHost = "${name}.${site}.grzb.de"; + targetUser = "colmena-deploy"; + }; + + # Set imports and optionally import colmena secrets configuration + imports = modules ++ nixpkgs.lib.optional (builtins.pathExists ./hosts/${name}/secrets.nix) ./hosts/${name}/secrets.nix; }; - mapToNixosConfigurations = name: host: generateNixosSystem name host; - - filterUnderscore = hosts: (nixpkgs.lib.filterAttrs (name: host: (builtins.substring 0 1 name) != "_") hosts); - - buildHosts = hosts: builtins.mapAttrs (name: host: host.config.system.build.toplevel) (filterUnderscore hosts); + generateNixConfiguration = name: { + hostNixpkgs, + system, + modules, + ... + }: + (nixpkgs.lib.filterAttrs (name: host: (builtins.substring 0 1 name) != "_") (hostNixpkgs.lib.nixosSystem { + inherit system modules; + })).config.system.build.toplevel; } diff --git a/hosts.nix b/hosts.nix new file mode 100644 index 0000000..083d5b8 --- /dev/null +++ b/hosts.nix @@ -0,0 +1,47 @@ +{ nixpkgs, nixpkgs-unstable, ... }: +let + environments = { + "proxmox" = [ + ./configuration/proxmox-vm + ./configuration/proxmox-vm/hardware-configuration.nix + ]; + }; + generateDefaults = hosts: builtins.mapAttrs (name: { + hostNixpkgs ? nixpkgs, + system ? "x86_64-linux", + pkgs ? hostNixpkgs.legacyPackages.${system}, + environment ? "proxmox", + site + }: { + inherit hostNixpkgs system pkgs environment site; + modules = [ + ./configuration/common + ./hosts/${name} + ] ++ (if environments ? ${environment} then environments.${environment} else []); + }) hosts; +in + generateDefaults { + hydra = { + hostNixpkgs = nixpkgs-unstable; + site = "vs"; + }; + iperf = { + site = "vs"; + }; + jackett = { + site = "vs"; + }; + nitter = { + site = "vs"; + }; + nixos-coturn = { + site = "vs"; + }; + tor-relay = { + site = "vs"; + }; + web-public-2 = { + hostNixpkgs = nixpkgs-unstable; + site = "vs"; + }; + }