Skip to content

Commit

Permalink
treewide: remove all uses of lib.pipe
Browse files Browse the repository at this point in the history
o7. It served us so well for so long, but thankfully now we have an actual pipe operator that does the same thing but with slightly more flexibility.

Alongside this treewide change is an improvement to the cleanliness of the cachix packages. Overall I'm not terribly happy with this approach _still_, especially since it doesn't really properly consider what derivations are actually needed and which aren't, instead it just blindly grabs what _might_ be useful. This isn't the best approach and I'd like to deal with it properly instead. One idea is to maybe have a "toplevel" attribute in the modules that will contain all relevant packages. The flake attributes for cachix can then leverage this module option to expose the useful things only.
  • Loading branch information
Frontear committed Dec 20, 2024
1 parent 88b1c16 commit ccacbb4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 79 deletions.
35 changes: 16 additions & 19 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
);

mkModules = (modulesPath: { ... } @ extraArgs: {
imports = final.pipe modulesPath [
final.filesystem.listFilesRecursive
(final.filter (final.hasSuffix "module.nix"))
(map (mod:
imports = final.filesystem.listFilesRecursive modulesPath
|> final.filter (final.hasSuffix "module.nix")
|> map (mod:
let
imported = import mod;
in {
Expand All @@ -22,12 +21,11 @@
_file = mod; # better error reporting in the module system
};
__functionArgs = final.functionArgs imported;
}))
];
});
});
});

mkNixOSConfigurations = (system: list: final.pipe list [
(map ({ hostName, modules, ... } @ extraArgs: {
mkNixOSConfigurations = (system: list: list
|> map ({ hostName, modules, ... } @ extraArgs: {
name = hostName;
value = final.nixosSystem {
specialArgs = {
Expand All @@ -42,19 +40,18 @@
}
] ++ modules;
} // (removeAttrs extraArgs [ "hostName" "modules" "specialArgs" ]);
}))
final.listToAttrs
]);
})
|> final.listToAttrs
);

mkPackages = (pkgs: pkgsPath: final.pipe pkgsPath [
final.filesystem.listFilesRecursive
(final.filter (final.hasSuffix "package.nix"))
(map (drv: rec {
mkPackages = (pkgs: pkgsPath: final.filesystem.listFilesRecursive pkgsPath
|> final.filter (final.hasSuffix "package.nix")
|> map (drv: rec {
name = final.getName value;
value = pkgs.callPackage drv {};
}))
final.listToAttrs
]);
})
|> final.listToAttrs
);

stripSystem = (system: flake:
let
Expand Down
11 changes: 8 additions & 3 deletions nix/nixosModules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ in {
../../users
];

config.home-manager.sharedModules = [
(lib.mkModules ../../modules/home-manager {})
];
config.home-manager = {
useGlobalPkgs = true;
useUserPackages = true;

sharedModules = [
(lib.mkModules ../../modules/home-manager {})
];
};
};
}
41 changes: 17 additions & 24 deletions nix/packages/cachix.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,22 @@
...
}:
{
# recursively update, as hosts may share a system
flake.packages = (with lib;
foldr recursiveUpdate {} (attrValues (mapAttrs (name: value:
let
inherit (value) config pkgs;
in {
# map to system.name for flake schema
${pkgs.system}.${name} = pkgs.linkFarmFromDrvs "cachix-${name}" (
# for modules/nixos
(pipe config.my [
attrValues
(filter (x: x ? package))
(map (x: x.package))
]) ++

# for modules/home-manager
(pipe config.home-manager.users [
attrValues
(map (x: attrValues x.my.programs))
concatLists
(filter (x: x ? package))
(map (x: x.package))
])
perSystem = { pkgs, ... }: {
packages = self.nixosConfigurations
|> lib.filterAttrs (_: value: value.pkgs.system == pkgs.system)
|> lib.mapAttrs (name: value: pkgs.linkFarmFromDrvs "cachix-${name}" (
# For modules/nixos
(lib.attrValues value.config.my
|> lib.filter (x: x ? package)
|> map (x: x.package))
++
# For modules/home-manager
(lib.attrValues value.config.home-manager.users
|> map (x: lib.attrValues x.my.programs)
|> lib.concatLists
|> lib.filter (x: x ? package)
|> map (x: x.package))
)
);
}) self.nixosConfigurations)));
};
}
59 changes: 26 additions & 33 deletions users/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,37 @@
...
}:
let
allUsers = lib.pipe ./. [
builtins.readDir
(lib.filterAttrs (_: type: type == "directory"))
(lib.mapAttrs (name: _: {
nixos = {
imports = [
./${name}/nixos
];
usersAttr = builtins.readDir ./.
|> lib.filterAttrs (name: _: name != "default.nix")
|> lib.mapAttrs (name: _: {
nixos = {
imports = [
./${name}/nixos
];

users.users."${name}" = {
inherit name;
home = "/home/${name}";
users.users."${name}" = {
inherit name;
home = "/home/${name}";

isNormalUser = true;
};
isNormalUser = true;
};
};

home = {
imports = [
./${name}/home
];
home = {
imports = [
./${name}/home
];

home = {
username = name;
homeDirectory = "/home/${name}";
};
home = {
username = name;
homeDirectory = "/home/${name}";
};
}))
];

mkUsers = {
imports = lib.mapAttrsToList (_: value: value.nixos) allUsers;

home-manager = {
useUserPackages = true;
useGlobalPkgs = true;

users = lib.mapAttrs (_: value: value.home) allUsers;
};
});
in {
imports = lib.mapAttrsToList (_: value: value.nixos) usersAttr;

config = {
home-manager.users = lib.mapAttrs (_: value: value.home) usersAttr;
};
in mkUsers
}

0 comments on commit ccacbb4

Please sign in to comment.