Skip to content

Commit

Permalink
Added more functions to custom lib. Cleaned up host creation functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
iivvaannxx committed Aug 31, 2023
1 parent e4266cf commit 72d29b3
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 152 deletions.
8 changes: 8 additions & 0 deletions lib/conditionals.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ in {

in value;

# If the given set contains the given attribute, that value is returned. Otherwise the fallback value is returned.
tryGetAttr = attr: set: fallback: let

hasAttribute = hasAttr attr set;
value = if hasAttribute then set.${attr} else fallback;

in value;

# Makes either one value or another depending on the given predicate.
mkIfElse = pred: truthy: falsy: mkMerge [

Expand Down
29 changes: 20 additions & 9 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@
includeLib = customLib: import customLib { inherit lib; };

# Include every function lib file.
global = includeLib ./global.nix;
conditionals = includeLib ./conditionals.nix;
filesystem = includeLib ./filesystem.nix;
generators = includeLib ./generators.nix;
global = includeLib ./global.nix;
make = includeLib ./make.nix;
nixos = includeLib ./nixos.nix;
options = includeLib ./options.nix;
validation = includeLib ./validation.nix;

in {

inherit (global)

importProfile
importCommonConfig
;

inherit (conditionals)

nullUnless
nullUnlessHasAttr
tryGetAttr

mkIfElse
mkIfHasAttr
Expand All @@ -40,18 +36,33 @@ in {
stringWithSuffix
;

inherit (global)

importProfile
importConfig
;

inherit (make)

mkPath
mkPkgs
mkUnfreePkgs
;

inherit (nixos)

mkHost
mkHostConfig
;

inherit (options)

mkBoolOption
mkStrOption
mkStrListOption

mkSubmoduleOption
mkDynamicAttrsetOption

mkPackageListOption
;

inherit (validation)
Expand Down
30 changes: 0 additions & 30 deletions lib/global.nix

This file was deleted.

33 changes: 33 additions & 0 deletions lib/make.nix
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);
}
181 changes: 77 additions & 104 deletions lib/nixos.nix
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;
};
}
];
};
}
Loading

0 comments on commit 72d29b3

Please sign in to comment.