config/lib/mkSystem.nix

130 lines
3.7 KiB
Nix
Raw Normal View History

2025-04-18 16:46:54 -07:00
# This function creates a NixOS system based on our VM setup for a
# particular architecture.
{
nixpkgs,
overlays,
inputs,
2025-05-12 15:19:01 -07:00
mkNeovim,
}:
name:
{
2025-05-07 10:22:05 -07:00
user, # ./users/{name}
2025-05-07 13:23:34 -07:00
host, # ./users/{name}/{host} (optional)
2025-05-07 10:22:05 -07:00
system, # arch-os
2025-05-12 15:19:01 -07:00
extraModules ? [ ],
}:
let
2025-05-07 10:22:05 -07:00
darwin = nixpkgs.lib.strings.hasSuffix "-darwin" system;
2025-05-12 15:19:01 -07:00
getInputModule = a: b: inputs.${a}.${if darwin then "darwinModules" else "nixosModules"}.${b};
2025-05-07 10:22:05 -07:00
# NixOS vs nix-darwin functions
2025-05-12 15:19:01 -07:00
systemFunc = if darwin then inputs.darwin.lib.darwinSystem else nixpkgs.lib.nixosSystem;
2025-05-07 10:22:05 -07:00
userDir = ../users + "/${user}";
userConfig = import (userDir + "/user.nix");
hostDir = userDir + "/${host}";
hostConfigPath = hostDir + "/configuration.nix";
userConfigPath = userDir + "/configuration.nix";
hostHomePath = hostDir + "/home.nix";
userHomePath = userDir + "/home.nix";
2025-05-12 15:19:01 -07:00
pathOrNull = a: if builtins.pathExists a then a else null;
2025-05-07 10:22:05 -07:00
# Arguments passed to all module files
args = {
inherit inputs;
currentSystem = system;
# Details about the host machine
host = {
inherit darwin name;
2025-05-12 15:19:01 -07:00
linux = !darwin;
2025-05-07 10:22:05 -07:00
};
2025-05-12 15:19:01 -07:00
user =
(
{
# This acts as formal documentation for what is allowed in user.nix
username, # unix username
name, # your display name
email, # for identity in programs such as git
dotfilesDir, # location to `../.`
timeZone ? "America/Los_Angeles",
# Stylix/Theming
theme ? null, # theme name for stylix
sexuality ? null, # pride flag for hyfetch
font ? null, # font to use
term, # preferred $TERM
editor, # preferred $EDITOR
browser ? null, # preferred $BROWSER
}@user:
user
)
userConfig;
2025-05-07 10:22:05 -07:00
};
systemSettings = rec {
2025-04-21 14:46:41 -07:00
inherit darwin;
2025-05-12 15:19:01 -07:00
homeDir = "/${if darwin then "Users" else "home"}/${userConfig.username}";
};
2025-05-12 15:19:01 -07:00
mainHomeImports = builtins.filter (f: f != null) [
(pathOrNull userHomePath)
(pathOrNull hostHomePath)
{
home.packages = [
(mkNeovim user system)
];
}
];
2025-04-18 16:46:54 -07:00
in
2025-05-12 15:19:01 -07:00
systemFunc {
inherit system;
2025-05-07 10:22:05 -07:00
2025-05-12 15:19:01 -07:00
modules =
builtins.filter (f: f != null) [
# Apply our overlays. Overlays are keyed by system type so we have
# to go through and apply our system type. We do this first so
# the overlays are available globally.
{ nixpkgs.overlays = overlays; }
2025-05-07 10:22:05 -07:00
2025-05-12 15:19:01 -07:00
# Modules shared between nix-darwin and NixOS
../modules/shared
# Modules for the specific OS
(if darwin then ../modules/macos else ../modules/nixos)
2025-05-07 10:22:05 -07:00
2025-05-12 15:19:01 -07:00
# The user-wide configuration.nix
(pathOrNull userConfigPath)
# The host-wide configuration.nix
(pathOrNull hostConfigPath)
2025-05-07 10:22:05 -07:00
2025-05-12 15:19:01 -07:00
# Set up nix-index and enable comma for easy one-shot command use
# https://github.com/nix-community/comma
(getInputModule "nix-index-database" "nix-index")
{ programs.nix-index-database.comma.enable = true; }
2025-05-07 10:22:05 -07:00
2025-05-12 15:19:01 -07:00
# Themes for all programs
(getInputModule "stylix" "stylix")
2025-05-07 10:22:05 -07:00
2025-05-12 15:19:01 -07:00
# Home manager
(getInputModule "home-manager" "home-manager")
{
home-manager = {
useGlobalPkgs = true;
useUserPackages = true;
backupFileExtension = "hm-backup";
# Arguments passed to all module files
extraSpecialArgs = args // {
inherit mainHomeImports;
2025-05-07 10:22:05 -07:00
};
2025-05-12 15:19:01 -07:00
# can't find how to make this an array without the param
users.${userConfig.username} = ../modules/home;
};
users.users.${userConfig.username}.home = systemSettings.homeDir;
}
2025-05-07 10:22:05 -07:00
2025-05-12 15:19:01 -07:00
# Arguments passed to all module files
{ config._module.args = args; }
]
# Add extra modules specified from config
++ extraModules;
}