Skip to content

Commit

Permalink
extract import to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
dszakallas committed Jan 28, 2025
1 parent ac46711 commit b38ca41
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 101 deletions.
76 changes: 20 additions & 56 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -100,6 +65,5 @@ rec {
};
};
};
in
outputs;
in outputs;
}
73 changes: 28 additions & 45 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit b38ca41

Please sign in to comment.