Users, use directory-driven users in flake.nix

This converts from the old manually defined users in flake.nix to a
dynamically defined users based on the directory structure. This is the
same as ba5fd48 but for users instead of hosts.
This commit is contained in:
Julia Lange 2024-11-25 09:52:05 -08:00
parent ba5fd48569
commit 0e5c6ab2c2
Signed by: Julia
SSH key fingerprint: SHA256:KI8YxpkPRbnDRkXPgCuQCVz181++Vy7NAvmQj8alOhM
3 changed files with 30 additions and 13 deletions

View file

@ -25,14 +25,18 @@
./nixosModules
] ++ extraModules;
};
hostFilter = { name, ...}: name == "host.nix";
hostPaths = fs.toList (fs.fileFilter hostFilter ./hosts);
hosts = builtins.listToAttrs (map (path: {
value = hostConfig [ path ];
name = builtins.unsafeDiscardStringContext (st.removeSuffix "/host.nix" (
builtins.elemAt (st.splitString "/hosts/" path) 1
));
hosts = let
hostFilter = { name, ...}: name == "host.nix";
hostPaths = fs.toList (fs.fileFilter hostFilter ./hosts);
# Assumes dir structure is start_of_path/hosts/hostname/host.nix
extractHostName = path: builtins.unsafeDiscardStringContext (
st.removeSuffix "/host.nix" (
builtins.elemAt (st.splitString "/hosts/" path) 1
)
);
in builtins.listToAttrs (map (path: {
value = path;
name = extractHostName path;
}) hostPaths);
userConfig = extraModules: home-manager.lib.homeManagerConfiguration {
@ -42,11 +46,24 @@
./hmModules
] ++ extraModules;
};
users = let
userFilter = { name, ...}: name == "user.nix";
userPaths = fs.toList (fs.fileFilter userFilter ./hosts);
in builtins.listToAttrs (map (path: let
dirsAndFiles = st.splitString "/" path;
dAFLength = builtins.length dirsAndFiles;
# Assumes dir structure is start_of_path/hosts/hostname/users/username/user.nix
hostname = builtins.unsafeDiscardStringContext (
builtins.elemAt dirsAndFiles (dAFLength - 4));
username = builtins.unsafeDiscardStringContext (
builtins.elemAt dirsAndFiles (dAFLength - 2));
in {
name = username + "@" + hostname;
value = path;
}
) userPaths);
in {
nixosConfigurations = hosts;
homeConfigurations."pan@onizuka" = userConfig [ ./hosts/onizuka/users/pan ];
homeConfigurations."pan@jibril" = userConfig [ ./hosts/jibril/users/pan ];
nixosConfigurations = builtins.mapAttrs (name: path: hostConfig [ path ]) hosts;
homeConfigurations = builtins.mapAttrs (name: path: userConfig [ path ]) users;
};
}