diff --git a/flake.nix b/flake.nix index 74b349b..a15242a 100644 --- a/flake.nix +++ b/flake.nix @@ -17,77 +17,42 @@ rec { }; nixConfig = { - extra-experimental-features = [ - "nix-command" - "flakes" - ]; - extra-substituters = [ - "https://nix-community.cachix.org" - "https://devenv.cachix.org" - ]; + extra-experimental-features = [ "nix-command" "flakes" ]; + extra-substituters = + [ "https://nix-community.cachix.org" "https://devenv.cachix.org" ]; extra-trusted-public-keys = [ "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=" "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=" ]; }; - outputs = - inputs@{ - self, - nix-darwin, - nixpkgs, - home-manager, - davids-dotfiles-private, - poetry2nix, - flake-utils, - ... - }: + outputs = inputs@{ self, nix-darwin, nixpkgs, home-manager + , davids-dotfiles-private, poetry2nix, flake-utils, ... }: let - importChildren = - d: builtins.mapAttrs (_: v: import v (inputs // outputs)) (outputs.davids-dotfiles.lib.subDirs d); - mkDarwin = - { host, arch, ... }: + mkDarwin = { host, arch, ... }: nix-darwin.lib.darwinSystem rec { system = "${arch}-darwin"; - specialArgs = - let - hostPlatform = nixpkgs.legacyPackages.${system}.stdenv.hostPlatform; - in - { - inherit - home-manager - hostPlatform - system - nixConfig - ; - }; + specialArgs = let + hostPlatform = nixpkgs.legacyPackages.${system}.stdenv.hostPlatform; + in { inherit home-manager hostPlatform system nixConfig; }; modules = [ home-manager.darwinModules.home-manager (import ./hosts/${host} (inputs // outputs)) - { - home-manager = { - extraSpecialArgs = specialArgs; - }; - } + { home-manager = { extraSpecialArgs = specialArgs; }; } ]; }; outputs = { davids-dotfiles = rec { lib = import ./lib { inherit (nixpkgs) lib; }; - darwinModules = importChildren ./modules/darwin; - systemModules = importChildren ./modules/system; - homeModules = importChildren ./modules/home; - users = importChildren ./users; - packages = ( - flake-utils.lib.eachDefaultSystem ( - system: - let - pkgs = nixpkgs.legacyPackages.${system}; - packages = lib.callPackageDirWith ./pkgs (inputs // pkgs); - in - packages - ) - ); + darwinModules = lib.importDir ./modules/darwin (inputs // outputs); + systemModules = lib.importDir ./modules/system (inputs // outputs); + homeModules = lib.importDir ./modules/home (inputs // outputs); + users = lib.importDir ./users (inputs // outputs); + packages = (flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + packages = lib.callPackageDirWith ./pkgs (inputs // pkgs); + in packages)); }; darwinConfigurations = { Jellyfish = mkDarwin { @@ -100,6 +65,5 @@ rec { }; }; }; - in - outputs; + in outputs; } diff --git a/lib/default.nix b/lib/default.nix index f0384b3..1c8a561 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,62 +1,45 @@ { lib, ... }: -with lib; -rec { +with lib; rec { # List immediate subdirectories of a directory - subDirs = - d: - foldlAttrs ( - a: k: v: - a // (if v == "directory" then { ${k} = d + "/${k}"; } else { }) - ) { } (builtins.readDir d); + subDirs = d: + foldlAttrs + (a: k: v: a // (if v == "directory" then { ${k} = d + "/${k}"; } else { })) + { } (builtins.readDir d); # Annotate a region of text for simpler identification of origin - textRegion = - { - name, - content, - comment-char ? "#", - }: - '' - ${comment-char} +begin ${name} - ${content} - ${comment-char} +end ${name} - ''; + textRegion = { name, content, comment-char ? "#", }: '' + ${comment-char} +begin ${name} + ${content} + ${comment-char} +end ${name} + ''; # Return the list of importable names in a directory. # Importable name is either # - a regular file with the nix extension # - or a directory containing default.nix. - importables = - root: - let - children = builtins.readDir root; - in - (builtins.attrNames ( - lib.attrsets.filterAttrs ( - n: t: - (t == "regular" && (builtins.match ".+\\.nix$" n) != null) - || (t == "directory" && builtins.pathExists (root + "/${n}/default.nix")) - ) children - )); + importables = root: + let children = builtins.readDir root; + in (builtins.attrNames (lib.attrsets.filterAttrs (n: t: + (t == "regular" && (builtins.match ".+\\.nix$" n) != null) + || (t == "directory" && builtins.pathExists (root + "/${n}/default.nix"))) + children)); + + # Import each importable in the dir and return as an attrset keyed by their names. + importDir = d: arg: + builtins.foldl' (a: name: a // { ${name} = import "${d}/${name}" arg; }) { } + (importables d); # Like callPackageWith but for a while directory. # The directory should contain nix files, or directories containing default.nix files that define packages. # The packages are imported with their file basename as the attribute name. - callPackageDirWith = - root: ins: + callPackageDirWith = root: ins: let callPkg = lib.callPackageWith all; - outs = builtins.listToAttrs ( - builtins.map (f: { - name = - let - m = (builtins.match "(.+)\\.nix$" f); - in - if m != null then builtins.head m else f; - value = callPkg (root + ("/" + f)) { }; - }) (importables root) - ); + outs = builtins.listToAttrs (builtins.map (f: { + name = let m = (builtins.match "(.+)\\.nix$" f); + in if m != null then builtins.head m else f; + value = callPkg (root + ("/" + f)) { }; + }) (importables root)); all = ins // outs; - in - outs; + in outs; }