-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more functions to custom lib. Cleaned up host creation functions.
- Loading branch information
1 parent
e4266cf
commit 72d29b3
Showing
6 changed files
with
157 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ lib, ... } @ args: let | ||
|
||
inherit (builtins) pathExists; | ||
inherit (lib) concatStringsSep throwIf; | ||
|
||
in rec { | ||
|
||
# Imports a package source and returns a package set. | ||
mkPkgs = sourcePkgs: { system, unfree ? false, overlays ? [ ] }: import sourcePkgs { | ||
|
||
inherit system overlays; | ||
config.allowUnfree = unfree; | ||
}; | ||
|
||
# Imports a package source and returns a package set with unfree packages enabled. | ||
mkUnfreePkgs = sourcePkgs: { system, overlays ? [ ] }: let | ||
|
||
unfreePkgs = mkPkgs sourcePkgs { | ||
|
||
inherit system overlays; | ||
unfree = true; | ||
}; | ||
|
||
in unfreePkgs; | ||
|
||
# Creates a full path from a source path and a sub path. Throws the given error if the path does not exist. | ||
mkPath = sourcePath: subPath: notFoundError: let | ||
|
||
fullPath = concatStringsSep "/" [sourcePath subPath]; | ||
isMissing = (! pathExists fullPath); | ||
|
||
in (throwIf isMissing notFoundError fullPath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,113 @@ | ||
{ lib }: let | ||
|
||
inherit (builtins) foldl' hasAttr pathExists baseNameOf; | ||
inherit (builtins) baseNameOf listToAttrs; | ||
|
||
inherit (lib) nixosSystem recursiveUpdate throwIf optionalAttrs mkMerge mkIf; | ||
inherit (lib.custom) getSubfolders mkIfHasAttr nullUnlessHasAttr; | ||
inherit (lib) nixosSystem; | ||
inherit (lib.custom) mkUnfreePkgs mkPath; | ||
|
||
# Traverses the given users folder and creates the users options for a system. | ||
mkUsers = usersFolder: homeManagerModules: let | ||
# Retrieves the profile of a user. | ||
getProfile = userPath : let | ||
|
||
# Each subfolder is a user. | ||
subfolders = getSubfolders usersFolder; | ||
baseUsers = { systemConfig = { }; homeConfig = { }; }; | ||
# Import the profile of the user. | ||
profilePath = mkPath userPath "profile.nix" "For user '${baseNameOf userPath}': Each user must define a 'profile.nix' file."; | ||
profile = (import profilePath); | ||
|
||
in foldl' (acc: folder: let | ||
in profile; | ||
|
||
mkPath = path: let | ||
# Generates entries for each system user. | ||
mkSystemUsers = users: { | ||
|
||
users.users = (listToAttrs (map (userPath: let | ||
|
||
# Construct the full path to the given file. | ||
fullPath = "${usersFolder}/${folder}/${path}"; | ||
isMissing = (! pathExists fullPath); | ||
# The profile of the current user. | ||
profile = getProfile userPath; | ||
|
||
in (throwIf isMissing "For user '${folder}': Each user must define a 'home.nix' and 'profile.nix' files." fullPath); | ||
in { | ||
|
||
profile = import (mkPath "profile.nix") { inherit lib; }; | ||
homeModule = mkPath "home.nix"; | ||
|
||
# Set fallbacks for these properties: | ||
username = if (hasAttr "username" profile) then profile.username else folder; | ||
fullName = if (hasAttr "fullName" profile) then profile.fullName else username; | ||
|
||
in { | ||
|
||
# The system configuration for the users. | ||
systemConfig = recursiveUpdate acc.systemConfig { | ||
|
||
users.users.${username} = { | ||
|
||
name = username; | ||
description = fullName; | ||
extraGroups = mkIfHasAttr "extraGroups" profile; | ||
name = profile.username; | ||
value = { | ||
|
||
name = profile.username; | ||
description = profile.fullName; | ||
isNormalUser = true; | ||
}; | ||
}; | ||
|
||
# The home configuration for the users. | ||
homeConfig = recursiveUpdate acc.homeConfig { | ||
|
||
${username} = { | ||
} // profile.systemUserOverride; | ||
|
||
imports = homeManagerModules ++ [ homeModule ]; | ||
}; | ||
}; | ||
|
||
}) baseUsers subfolders; | ||
}) users)); | ||
}; | ||
|
||
# Creates a system configuration. | ||
mkSystem = { system, configPath, usersPath, modules, home-manager, extraSpecialArgs ? { } }: let | ||
# Generates entries for each home manager user. | ||
mkHomeUsers = homeUsers: (listToAttrs (map (userPath: let | ||
|
||
# Make the users for this system. | ||
users = mkUsers usersPath modules.homeManagerModules; | ||
# The profile of the current user. | ||
profile = getProfile userPath; | ||
|
||
in { | ||
|
||
inherit system lib; | ||
modules = modules.nixosModules ++ [ | ||
|
||
configPath | ||
users.systemConfig | ||
|
||
# Add the HM module. See: https://github.com/nix-community/home-manager/ | ||
home-manager.nixosModules.home-manager { | ||
name = profile.username; | ||
value = { | ||
|
||
_module.args.profile = profile; | ||
imports = [ userPath ]; | ||
}; | ||
|
||
home-manager.useGlobalPkgs = true; | ||
home-manager.useUserPackages = true; | ||
|
||
home-manager.extraSpecialArgs = { } // extraSpecialArgs; | ||
home-manager.users = users.homeConfig; | ||
} | ||
]; | ||
}; | ||
|
||
# Creates a host module definition. | ||
mkHostConfig = { system, hostPath, modules, inputs, extraArgs ? { } } @ args: let | ||
}) homeUsers)); | ||
|
||
hostName = baseNameOf hostPath; | ||
mkPath = path: isFile: let | ||
in { | ||
|
||
fullPath = "${hostPath}/${path}"; | ||
isMissing = (! pathExists fullPath); | ||
resource = if isFile then "file" else "folder"; | ||
# Creates a host from the given arguments. | ||
mkHost = { hostPath, system, users, inputs, lib, nixpkgs, unstablepkgs, home-manager }: let | ||
|
||
in (throwIf isMissing "For host '${hostName}': A '${path}' ${resource} must be created for the host to be built." fullPath); | ||
inherit (lib) nixosSystem; | ||
|
||
# Creates a preconfigured Nix package provider. | ||
mkPkgs = system: pkgs: let | ||
# The path where all the source code is located. | ||
rootPath = ../.; | ||
configsPath = "${rootPath}/configs"; | ||
|
||
pkgsSource = import pkgs { | ||
# The package sets. | ||
pkgs = mkUnfreePkgs nixpkgs { inherit system; }; | ||
upkgs = mkUnfreePkgs unstablepkgs { inherit system; }; | ||
|
||
# Resources used within NixOS configurations. | ||
nixosModules = (import "${rootPath}/modules/nixos"); | ||
nixosUsers = mkSystemUsers users; | ||
nixosSpecialArgs = { | ||
|
||
inherit system; | ||
config.allowUnfree = true; | ||
}; | ||
|
||
in pkgsSource; | ||
inherit inputs configsPath pkgs upkgs; | ||
presetsPath = "${rootPath}/presets/nixos"; | ||
}; | ||
|
||
# The two paths needed by mkSystem. | ||
configPath = mkPath "configs/system.nix" true; | ||
usersPath = mkPath "users" false; | ||
# Resources used within Home Manager configurations. | ||
homeManagerModules = (import "${rootPath}/modules/home-manager"); | ||
homeManagerUsers = mkHomeUsers users; | ||
homeManagerSpecialArgs = { | ||
|
||
nixpkgs = nullUnlessHasAttr "nixpkgs" inputs; | ||
unstablepkgs = nullUnlessHasAttr "unstablepkgs" inputs; | ||
pkgs = optionalAttrs (nixpkgs != null) (mkPkgs system nixpkgs); | ||
upkgs = optionalAttrs (unstablepkgs != null) (mkPkgs system unstablepkgs); | ||
inherit inputs configsPath pkgs upkgs; | ||
presetsPath = "${rootPath}/presets/home-manager"; | ||
}; | ||
|
||
in nixosSystem { | ||
|
||
# Ensure home-manager is inside the inputs. | ||
home-manager = throwIf (! hasAttr "home-manager" inputs) | ||
"For host '${hostName}': 'home-manager' is required to build the host. Please make sure is inside the flake inputs." | ||
inputs.home-manager; | ||
inherit system lib; | ||
|
||
# Pass the inputs as extra args together with the given ones. | ||
extraSpecialArgs = ((mkMerge [ | ||
specialArgs = nixosSpecialArgs; | ||
modules = nixosModules ++ [ | ||
|
||
{ inherit inputs; } | ||
hostPath | ||
nixosUsers | ||
|
||
# Only include the packages if the input is correctly defined. | ||
(if (nixpkgs != null) then { inherit pkgs; } else { }) | ||
(if (unstablepkgs != null) then { inherit upkgs; } else { }) | ||
|
||
]) // extraArgs); | ||
|
||
in (mkSystem { inherit system configPath usersPath modules extraSpecialArgs home-manager; }); | ||
home-manager.nixosModules.home-manager { | ||
|
||
# Creates a host definition ready to use in a flake as a nixosConfiguration. | ||
mkHost = hostConfig: (nixosSystem hostConfig); | ||
home-manager = { | ||
|
||
in { | ||
useGlobalPkgs = true; | ||
useUserPackages = true; | ||
|
||
inherit mkHostConfig mkHost; | ||
users = homeManagerUsers; | ||
sharedModules = homeManagerModules; | ||
extraSpecialArgs = homeManagerSpecialArgs; | ||
}; | ||
} | ||
]; | ||
}; | ||
} |
Oops, something went wrong.