From 881d9577b8c035e36a6703ce346dd2e588557f18 Mon Sep 17 00:00:00 2001 From: Weijia Wang <9713184+wegank@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:03:12 +0200 Subject: [PATCH] flake.nix: make more non-recursive (#263) * flake.nix: make more non-recursive Co-authored-by: Valentin Gagarin --- flake.nix | 151 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 78 insertions(+), 73 deletions(-) diff --git a/flake.nix b/flake.nix index 85f498da..3b375dce 100644 --- a/flake.nix +++ b/flake.nix @@ -75,20 +75,16 @@ inherit dream2nix pkgs; }; + overlay = final: prev: importNgiPackages prev; + # NGI projects are imported from ./projects/default.nix. - # Each project includes packages, and optionally, modules, configurations and tests. - importNgiProjects = { - pkgs ? {}, - sources ? { - examples = rawExamples; - modules = extendedNixosModules; - }, - }: - import ./projects {inherit lib pkgs sources;}; - - # The above definition reimports `rawExamples` and `extendedNixosModules` into `sources`. - # As configurations and modules are system-agnostic, they are defined by passing `{}` to `importNgiProjects`. - rawNgiProjects = importNgiProjects {}; + # Each project includes packages, and optionally, modules, examples and tests. + + # Note that modules and examples are system-agnostic, so import them first. + rawNgiProjects = import ./projects { + inherit lib; + sources = {}; + }; rawExamples = flattenAttrsSlash (mapAttrs (_: v: mapAttrs (_: v: v.path) v) ( mapAttrByPath ["nixos" "examples"] {} rawNgiProjects @@ -98,8 +94,18 @@ mapAttrByPath ["nixos" "modules"] {} rawNgiProjects ))); + nixosModules = + { + unbootable = ./modules/unbootable.nix; + # The default module adds the default overlay on top of Nixpkgs. + # This is so that `ngipkgs` can be used alongside `nixpkgs` in a configuration. + default.nixpkgs.overlays = [overlay]; + } + // (filterAttrs (_: v: v != null) rawNixosModules); + + # Next, extend the modules with the sops-nix module, used in the tests. extendedNixosModules = - self.nixosModules + nixosModules // { sops-nix = sops-nix.nixosModules.default; }; @@ -109,15 +115,66 @@ (_: config: nixosSystem {modules = [config ./dummy.nix] ++ attrValues extendedNixosModules;}) rawExamples; - # Then, define the system-specific outputs. + # Then, import packages and tests, which are system-dependent. + importNgiProjects = pkgs: + import ./projects { + inherit lib pkgs; + sources = { + examples = rawExamples; + modules = extendedNixosModules; + }; + }; + + # Finally, define the system-agnostic outputs. + systemAgnosticOutputs = { + nixosConfigurations = + extendedNixosConfigurations + // { + makemake = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + + modules = [ + # Use NixOS module for pinned Hydra, but note that this doesn't + # set the package to be from that repo. It juse uses the stock + # `pkgs.hydra_unstable` by default. + hydra.nixosModules.hydra + + # Setup both a master and a worker buildbot instance in this host + buildbot-nix.nixosModules.buildbot-master + buildbot-nix.nixosModules.buildbot-worker + + { + # Here, set the Hydra package to use the (complete + # self-contained, pinning nix, nixpkgs, etc.) default Hydra + # build. Other than this one package, those pins versions are + # not used. + services.hydra.package = hydra.packages.x86_64-linux.default; + } + + sops-nix.nixosModules.default + + ./infra/makemake/configuration.nix + + { + #nix.registry.nixpkgs.flake = nixpkgs; + nix.nixPath = ["nixpkgs=${nixpkgs}"]; + } + ]; + }; + }; + + inherit nixosModules; + + # Overlays a package set (e.g. Nixpkgs) with the packages defined in this flake. + overlays.default = overlay; + }; + eachDefaultSystemOutputs = flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs {inherit system;}; ngiPackages = importNgiPackages pkgs; - ngiProjects = importNgiProjects { - pkgs = pkgs // ngiPackages; - }; + ngiProjects = importNgiProjects (pkgs // ngiPackages); toplevel = name: config: nameValuePair "nixosConfigs/${name}" config.config.system.build.toplevel; @@ -136,7 +193,7 @@ system.stateVersion = "23.05"; } ] - ++ attrValues self.nixosModules; + ++ attrValues nixosModules; }) .options; }; @@ -200,7 +257,7 @@ pkgs = import nixpkgs { inherit system; - overlays = [self.overlays.default]; + overlays = [overlay]; }; # Include all packages (with overview and options) @@ -210,9 +267,7 @@ # As a workaround, consider packages with empty meta as non-broken. nonBrokenNgiPackages = filterAttrs (_: v: !(attrByPath ["meta" "broken"] false v)) ngiPackages; - nonBrokenNgiProjects = importNgiProjects { - pkgs = pkgs // nonBrokenNgiPackages; - }; + nonBrokenNgiProjects = importNgiProjects (pkgs // nonBrokenNgiPackages); in { # buildbot executes `nix flake check`, therefore this output # should only contain derivations that can built within CI. @@ -241,56 +296,6 @@ extendedNixosConfigurations; }; }; - - systemAgnosticOutputs = { - nixosConfigurations = - extendedNixosConfigurations - // { - makemake = nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - - modules = [ - # Use NixOS module for pinned Hydra, but note that this doesn't - # set the package to be from that repo. It juse uses the stock - # `pkgs.hydra_unstable` by default. - hydra.nixosModules.hydra - - # Setup both a master and a worker buildbot instance in this host - buildbot-nix.nixosModules.buildbot-master - buildbot-nix.nixosModules.buildbot-worker - - { - # Here, set the Hydra package to use the (complete - # self-contained, pinning nix, nixpkgs, etc.) default Hydra - # build. Other than this one package, those pins versions are - # not used. - services.hydra.package = hydra.packages.x86_64-linux.default; - } - - sops-nix.nixosModules.default - - ./infra/makemake/configuration.nix - - { - #nix.registry.nixpkgs.flake = nixpkgs; - nix.nixPath = ["nixpkgs=${nixpkgs}"]; - } - ]; - }; - }; - - nixosModules = - { - unbootable = ./modules/unbootable.nix; - # The default module adds the default overlay on top of Nixpkgs. - # This is so that `ngipkgs` can be used alongside `nixpkgs` in a configuration. - default.nixpkgs.overlays = [self.overlays.default]; - } - // (filterAttrs (_: v: v != null) rawNixosModules); - - # Overlays a package set (e.g. Nixpkgs) with the packages defined in this flake. - overlays.default = final: prev: importNgiPackages prev; - }; in foldr recursiveUpdate {} [ eachDefaultSystemOutputs