Documentation for lib/systems

Additionally:
- fix a typo in lib/default with the type hinting
- fix a likely breaking change with lib.rad-dev.genNonX86, as it was
returning an attrset instead of a list of attrsets
- rename var to cond in lib.rad-dev.genWrapper, to make it clearer that
a condition is expected

Signed-off-by: ahuston-0 <aliceghuston@gmail.com>
This commit is contained in:
ahuston-0 2024-05-19 16:04:57 -04:00 committed by Alice Huston
parent 63ca84cfb8
commit db7aeb7f03
2 changed files with 134 additions and 25 deletions

View File

@ -20,7 +20,7 @@
# set: attrset to search # set: attrset to search
# #
# type: # type:
# mapGetAttr :: String -> Attrset -> [Any] # mapGetAttr :: String -> AttrSet -> [Any]
mapGetAttr = (attr: set: lib.mapAttrsToList (_: attrset: lib.getAttr attr attrset) set); mapGetAttr = (attr: set: lib.mapAttrsToList (_: attrset: lib.getAttr attr attrset) set);
# gets list of files and directories inside of a directory # gets list of files and directories inside of a directory

View File

@ -1,8 +1,26 @@
{ lib, ... }: { lib, ... }:
rec { rec {
# Creates the hostname attrset given a hostname
#
# args:
# hostname: hostname of the machine
#
# type:
# genHostName :: String -> AttrSet
genHostName = hostname: { config.networking.hostName = hostname; }; genHostName = hostname: { config.networking.hostName = hostname; };
# Imports home-manager config for each user, as well as the general home-manager NixOS module
#
# The args are passed in as an AttrSet
#
# args:
# inputs: flake-level inputs, for use in the modules
# users: list of users to import
# src: base path of the flake
#
# type:
# genHome :: AttrSet -> [AttrSet]
genHome = genHome =
{ {
inputs, inputs,
@ -13,6 +31,17 @@ rec {
[ inputs.home-manager.nixosModules.home-manager ] [ inputs.home-manager.nixosModules.home-manager ]
++ (map (user: { home-manager.users.${user} = import (src + "/users/${user}/home.nix"); }) users); ++ (map (user: { home-manager.users.${user} = import (src + "/users/${user}/home.nix"); }) users);
# Imports password for each user via SOPS, as well as the general SOPS NixOS module
#
# The args are passed in as an AttrSet
#
# args:
# inputs: flake-level inputs, for use in the modules
# users: list of users to import
# src: base path of the flake
#
# type:
# genSops :: AttrSet -> [AttrSet]
genSops = genSops =
{ {
inputs, inputs,
@ -28,6 +57,14 @@ rec {
}; };
}) users); }) users);
# Imports config for a given user
#
# args:
# user: user to generate the config for
# src: base path of the flake
#
# type:
# importUser :: String -> Path -> (AttrSet -> AttrSet)
importUser = importUser =
user: src: user: src:
{ {
@ -40,36 +77,86 @@ rec {
users.users.${user} = import (src + "/users/${user}") (args // { name = user; }); users.users.${user} = import (src + "/users/${user}") (args // { name = user; });
}; };
# Imports the user configs for a list of users
#
# The args are passed in as an AttrSet
#
# args:
# users: list of users to import
# src: base path of the flake
#
# type:
# genUsers :: AttrSet -> [AttrSet]
genUsers = { users, src, ... }: (map (user: importUser user src) users); genUsers = { users, src, ... }: (map (user: importUser user src) users);
# Adds a config option for machines which are not x86_64-linux
#
# Note: the args are passed as an AttrSet for compatibility with genWrapper,
# none of the args are actually used
#
# type:
# genNonX86 :: AttrSet -> [AttrSet]
genNonX86 = genNonX86 =
{ ... }: { ... }:
[
{ {
config.nixpkgs = { config.nixpkgs = {
config.allowUnsupportedSystem = true; config.allowUnsupportedSystem = true;
buildPlatform = "x86_64-linux"; buildPlatform = "x86_64-linux";
}; };
}; }
];
# A wrapper for optionally generating configs based on arguments to constructSystem
#
# args:
# cond: condition to generate based on
# func: function to generate a module
# args: inputs to the module described by func
#
# type:
# genWrapper :: Boolean -> (AttrSet -> [AttrSet])
genWrapper = genWrapper =
var: func: args: cond: func: args:
lib.optionals var (func args); lib.optionals cond (func args);
# Makes a custom NixOS system
#
# The args are passed in as an AttrSet
#
# args:
# configPath: path to the folder containing hardware.nix & configuration.nix
# hostname: hostname of the server
# inputs: flake inputs to be used
# src: base path of the repo
# users: list of users to be added
# home: enables home-manager on this machine (requires all users to have home-manager)
# modules: list of machine-specific modules
# server: determines if this machine is a server (true) or a PC (false)
# sops: enables sops on this machine
# system: the system architecture of the machine
#
# Adds extra common modules!
# - SuperSandro2000's nixos-modules, for convenience functions and optional opinionated configs
# - hardware.nix and configuration.nix, as one would expect for a typical setup
# - the modules/ directory (check it out! most options can be overridden or are opt-in)
# - convenience functions for SOPS, home-manager, user generation, and handling non-x86 machines
#
# type:
# constructSystem :: AttrSet -> AttrSet
constructSystem = constructSystem =
{ {
configPath,
hostname, hostname,
users,
inputs, inputs,
src, src,
users,
home ? true, home ? true,
iso ? [ ],
modules ? [ ], modules ? [ ],
server ? true, server ? true,
sops ? true, sops ? true,
system ? "x86_64-linux", system ? "x86_64-linux",
}@args: }@args:
lib.nixosSystem { lib.nixosSystem {
inherit system; inherit system;
specialArgs = inputs; specialArgs = inputs;
@ -77,8 +164,8 @@ rec {
[ [
inputs.nixos-modules.nixosModule inputs.nixos-modules.nixosModule
(genHostName hostname) (genHostName hostname)
(src + "/systems/${hostname}/hardware.nix") (configPath + "/hardware.nix")
(src + "/systems/${hostname}/configuration.nix") (configPath + "/configuration.nix")
] ]
++ modules ++ modules
++ (lib.rad-dev.fileList (src + "/modules")) ++ (lib.rad-dev.fileList (src + "/modules"))
@ -88,18 +175,40 @@ rec {
++ genWrapper (system != "x86_64-linux") genNonX86 args; ++ genWrapper (system != "x86_64-linux") genNonX86 args;
}; };
# a convenience function for automatically generating NixOS systems by reading a directory via constructSystem
#
# Note: if you are only generating one system or there is no
# folder/<hostname>/{default,configuration,hardware}.nix structure, you are
# better off either directly invoking constructSystem or lib.nixosSystem
#
# args:
# inputs: flake-inputs to be distributed to each system config
# src: the base path to the repo
# path: the path to read the systems from, should be a directory containing one directory per machine, each having at least the following
# - default.nix (with the extra params for constructSystem in it, see systems/palatine-hill/default.nix for an example)
# - hardware.nix
# - configuration.nix
#
# type:
# genSystems :: AttrSet -> Path -> Path -> AttrSet
genSystems = genSystems =
inputs: src: path: inputs: src: path:
builtins.listToAttrs ( builtins.listToAttrs (
map (name: { map (
name:
let
configPath = path + "/${name}";
in
{
inherit name; inherit name;
value = constructSystem ( value = constructSystem (
{ {
inherit inputs src; inherit inputs src configPath;
hostname = name; hostname = name;
} }
// import (path + "/${name}") { inherit inputs; } // import configPath { inherit inputs; }
); );
}) (lib.rad-dev.lsdir path) }
) (lib.rad-dev.lsdir path)
); );
} }