diff --git a/default.nix b/default.nix new file mode 100644 index 000000000..85ffae806 --- /dev/null +++ b/default.nix @@ -0,0 +1,7 @@ +{system ? builtins.currentSystem}: let + # Import nixpkgs and dream2nix + helpers = import ./helpers.nix {inherit system;}; + inherit (helpers) importNgiPackages pkgs; + ngiPackages = importNgiPackages pkgs; +in + ngiPackages diff --git a/flake-inputs.nix b/flake-inputs.nix new file mode 100644 index 000000000..46d7d763b --- /dev/null +++ b/flake-inputs.nix @@ -0,0 +1,17 @@ +{system ? builtins.currentSystem}: let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + + # Import flake inputs using flake-compat from dream2nix + flake = let + dream2nix = with lock.nodes.dream2nix.locked; + fetchTarball { + url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz"; + sha256 = narHash; + }; + in + import (dream2nix + "/dev-flake/flake-compat.nix") { + inherit system; + src = ./.; + }; +in + flake.defaultNix.inputs diff --git a/helpers.nix b/helpers.nix new file mode 100644 index 000000000..87fee4a99 --- /dev/null +++ b/helpers.nix @@ -0,0 +1,77 @@ +{ + system ? builtins.currentSystem, + inputs ? import ./flake-inputs.nix {inherit system;}, + nixpkgs ? inputs.nixpkgs, + dream2nix ? inputs.dream2nix, + sops-nix ? inputs.sops-nix, +}: let + pkgs = import nixpkgs.outPath {inherit system;}; + + lib = pkgs.lib.recursiveUpdate pkgs.lib (import ./lib.nix {inherit (pkgs) lib;}); + + helpers = rec { + # NGI packages are imported from ./pkgs/by-name/default.nix. + importNgiPackages = pkgs: let + callPackage = pkgs.newScope ( + ngiPackages // {inherit callPackage;} + ); + + ngiPackages = import ./pkgs/by-name { + inherit (pkgs) lib; + inherit callPackage pkgs; + dream2nix = import dream2nix.outPath {inherit pkgs;}; + }; + in + ngiPackages; + + # NGI projects are imported from ./projects/default.nix. + # Each project includes packages, and optionally, modules, configurations and tests. + importNgiProjects = pkgs: let + ngiProjects = import ./projects { + inherit lib pkgs; + sources = { + configurations = rawNixosConfigurations; + modules = extendedNixosModules; + }; + }; + in + ngiProjects; + + # The above definition reimports `rawNixosConfigurations` and `extendedNixosModules` into `sources`. + # As configurations and modules are system-agnostic, they are defined by passing `{}` to `importNgiProjects`. + rawNgiProjects = importNgiProjects {}; + + rawNixosConfigurations = lib.flattenAttrsSlash (lib.mapAttrs (_: v: lib.mapAttrs (_: v: v.path) v) ( + lib.mapAttrByPath ["nixos" "configurations"] {} rawNgiProjects + )); + + rawNixosModules = lib.flattenAttrsDot (lib.foldl lib.recursiveUpdate {} (lib.attrValues ( + lib.mapAttrByPath ["nixos" "modules"] {} rawNgiProjects + ))); + + rawOutputs = rec { + 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 = [overlays.default]; + } + // (lib.filterAttrs (_: v: v != null) rawNixosModules); + + overlays.default = final: prev: importNgiPackages prev; + }; + + extendedNixosModules = + rawOutputs.nixosModules + // { + sops-nix = sops-nix.nixosModules.default; + }; + + extendedNixosConfigurations = + lib.mapAttrs + (_: config: lib.nixosSystem {modules = [config ./dummy.nix] ++ lib.attrValues extendedNixosModules;}) + rawNixosConfigurations; + }; +in + helpers // {inherit inputs pkgs;}