From 4b8609385a8d835f95b5295b785e070bb29017e4 Mon Sep 17 00:00:00 2001 From: Alejandro Soto Date: Sun, 26 Jan 2025 15:07:06 -0600 Subject: [PATCH] flake: initial commit --- flake.nix | 174 +++++++++++++++++++++++++++++++++++++++++ hm/default.nix | 1 + nixos/default.nix | 1 + pkgs/default.nix | 8 ++ pkgs/lib/default.nix | 3 + pkgs/lib/importAll.nix | 20 +++++ 6 files changed, 207 insertions(+) create mode 100644 flake.nix create mode 100644 hm/default.nix create mode 100644 nixos/default.nix create mode 100644 pkgs/default.nix create mode 100644 pkgs/lib/default.nix create mode 100644 pkgs/lib/importAll.nix diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..05ffad2 --- /dev/null +++ b/flake.nix @@ -0,0 +1,174 @@ +{ + inputs.flake-utils.url = "github:numtide/flake-utils"; + + outputs = { + self, + nixpkgs, + flake-utils, + }: let + mapOverlayOverride = namespace: overlay: final: prev: let + overlayPkgs = overlay final prev; + in + { + "${namespace}" = builtins.removeAttrs overlayPkgs ["override"]; + } + // (overlayPkgs.override or {}); + in + flake-utils.lib.eachDefaultSystem (system: { + formatter = (import nixpkgs {inherit system;}).alejandra; + }) + // { + overlays = let + overlay = mapOverlayOverride "trivium" (import ./pkgs); + in { + default = overlay; + trivium = overlay; + }; + + lib = { + mkSystemFlake = { + flakes, + system, + formatter ? "alejandra", + localOverlayPath ? null, + nixpkgsConfigPath ? null, + nixosSourcePath ? null, + nixosPlatformsPath ? + if nixosSourcePath != null + then nixosSourcePath + /platform + else null, + hmSourcePath ? null, + hmPlatformsPath ? + if hmSourcePath != null + then hmSourcePath + /platform + else null, + }: let + optionalFlake = name: + if flakes ? "${name}" + then flakes.${name} + else null; + + requireFlake = name: + if flakes ? "${name}" + then flakes.${name} + else throw "Required flake input '${name}' is required but was not provided"; + + nur = optionalFlake "nur"; + nixpkgs = requireFlake "nixpkgs"; + unstable = optionalFlake "unstable"; + + home-manager = + if hmSourcePath != null + then requireFlake "home-manager" + else null; + + pkgs = importPkgs nixpkgs; + + importPkgs = flake: + import flake ({ + inherit system; + + config = import ./pkgs/config nixpkgs.lib; + overlays = let + conditions = [ + { + overlay = nur.overlays.default; + condition = nur != null; + } + # NB: Preserve the relative order + { + overlay = self.overlays.trivium; + condition = true; + } + { + overlay = flakes.self.overlays.default; + condition = true; + } + ]; + in + builtins.map (cond: cond.overlay) (builtins.filter (cond: cond.condition) conditions); + } + // ( + if nixpkgsConfigPath != null + then { + config = import nixpkgsConfigPath {inherit (nixpkgs) lib;}; + } + else {} + )); + + inherit (pkgs.trivium.lib) importAll; + in + with pkgs.lib; + { + formatter.${system} = + if formatter == "alejandra" + then pkgs.alejandra + else if formatter == "nixpkgs-fmt" + then pkgs.nixpkgs-fmt + else throw "Unknown formatter: '${formatter}'"; + + packages.${system} = pkgs.local; + + overlays.default = final: prev: let + overlay = + if localOverlayPath != null + then import localOverlayPath + else (final: prev: {}); + in + mapOverlayOverride "local" overlay final prev + // optionalAttrs (unstable != null) { + unstable = importPkgs unstable; + }; + } + // optionalAttrs (nixosSourcePath != null) { + nixosConfigurations = let + nixosSystem = {modules}: + makeOverridable nixpkgs.lib.nixosSystem { + inherit modules pkgs system; + + specialArgs = { + inherit flakes; + }; + }; + + hostConfig = platform: + nixosSystem { + modules = [ + ./nixos + nixosSourcePath + platform + ]; + }; + in + mapAttrs (_: hostConfig) (importAll {root = nixosPlatformsPath;}); + } + // optionalAttrs (hmSourcePath != null) { + homeConfigurations = let + registry = {...}: { + config.nix.registry = + mapAttrs + (_: value: {flake = value;}) + flakes; + }; + + home = name: platform: + home-manager.lib.homeManagerConfiguration { + inherit pkgs; + + extraSpecialArgs = { + inherit flakes; + }; + + modules = [ + ./hm + hmSourcePath + platform + registry + ]; + }; + in + mapAttrs home (importAll {root = hmPlatformsPath;}); + }; + }; + }; +} diff --git a/hm/default.nix b/hm/default.nix new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/hm/default.nix @@ -0,0 +1 @@ +{} diff --git a/nixos/default.nix b/nixos/default.nix new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/nixos/default.nix @@ -0,0 +1 @@ +{} diff --git a/pkgs/default.nix b/pkgs/default.nix new file mode 100644 index 0000000..2ee6bfd --- /dev/null +++ b/pkgs/default.nix @@ -0,0 +1,8 @@ +final: prev: +with prev.lib; let + inherit (final) callPackage; +in { + lib = callPackage ./lib {}; + + override = {}; +} diff --git a/pkgs/lib/default.nix b/pkgs/lib/default.nix new file mode 100644 index 0000000..e3ab56e --- /dev/null +++ b/pkgs/lib/default.nix @@ -0,0 +1,3 @@ +{callPackage}: { + importAll = callPackage ./importAll.nix {}; +} diff --git a/pkgs/lib/importAll.nix b/pkgs/lib/importAll.nix new file mode 100644 index 0000000..6acabe9 --- /dev/null +++ b/pkgs/lib/importAll.nix @@ -0,0 +1,20 @@ +{lib}: { + root, + exclude ? [], +}: +with builtins; +with lib; +# http://chriswarbo.net/projects/nixos/useful_hacks.html + let + basename = removeSuffix ".nix"; + + isMatch = name: type: + (hasSuffix ".nix" name || type == "directory") + && ! elem name (map basename exclude); + + entry = name: _: { + name = basename name; + value = import (root + "/${name}"); + }; + in + mapAttrs' entry (filterAttrs isMatch (readDir root))