feat(modules): core option system and shared lib helpers
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
87bfc4cc31
commit
3b21d570e0
3 changed files with 887 additions and 0 deletions
202
modules/lib.nix
Normal file
202
modules/lib.nix
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
{
|
||||
config,
|
||||
pkgs,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
|
||||
# Shared library module for Omnixient
|
||||
# Exposes helpers via config.omni.lib for use by all other modules.
|
||||
# This replaces the old import-based helpers.nix pattern.
|
||||
|
||||
let
|
||||
inherit (lib)
|
||||
mkIf
|
||||
mkOption
|
||||
types
|
||||
concatStringsSep
|
||||
mapAttrsToList
|
||||
;
|
||||
cfg = config.omni;
|
||||
|
||||
helpers = {
|
||||
# Check if a feature is enabled
|
||||
isEnabled = feature: cfg.features.${feature} or false;
|
||||
|
||||
# User-specific paths
|
||||
userPath = path: "/home/${cfg.user}/${path}";
|
||||
configPath = path: "/home/${cfg.user}/.config/${path}";
|
||||
cachePath = path: "/home/${cfg.user}/.cache/${path}";
|
||||
|
||||
# Color helper: use nix-colors if available, otherwise fallback
|
||||
getColor =
|
||||
colorName: fallback:
|
||||
if cfg.colorScheme != null && cfg.colorScheme ? colors && cfg.colorScheme.colors ? ${colorName} then
|
||||
"#${cfg.colorScheme.colors.${colorName}}"
|
||||
else
|
||||
fallback;
|
||||
|
||||
# Feature-based conditional inclusion
|
||||
withFeature = feature: content: mkIf (helpers.isEnabled feature) content;
|
||||
withoutFeature = feature: content: mkIf (!(helpers.isEnabled feature)) content;
|
||||
|
||||
# User-specific home-manager configuration
|
||||
forUser = userConfig: {
|
||||
home-manager.users.${cfg.user} = userConfig;
|
||||
};
|
||||
|
||||
# Package filtering with exclusion support
|
||||
filterPackages =
|
||||
packages:
|
||||
builtins.filter (
|
||||
pkg:
|
||||
let
|
||||
name = pkg.name or pkg.pname or "unknown";
|
||||
in
|
||||
!(builtins.elem name (cfg.packages.exclude or [ ]))
|
||||
) packages;
|
||||
|
||||
# Create a script with set -euo pipefail
|
||||
makeScript =
|
||||
name: description: script:
|
||||
pkgs.writeShellScriptBin name ''
|
||||
#!/usr/bin/env bash
|
||||
# ${description}
|
||||
set -euo pipefail
|
||||
${script}
|
||||
'';
|
||||
|
||||
# Standard paths
|
||||
paths = {
|
||||
config = "/etc/nixos";
|
||||
logs = "/var/log/omni";
|
||||
cache = "/var/cache/omni";
|
||||
runtime = "/run/omni";
|
||||
};
|
||||
|
||||
# Default color palette (Tokyo Night)
|
||||
colors = {
|
||||
bg = "#1a1b26";
|
||||
bgAlt = "#16161e";
|
||||
bgAccent = "#2f3549";
|
||||
fg = "#c0caf5";
|
||||
fgAlt = "#9aa5ce";
|
||||
fgDim = "#545c7e";
|
||||
red = "#f7768e";
|
||||
orange = "#ff9e64";
|
||||
yellow = "#e0af68";
|
||||
green = "#9ece6a";
|
||||
cyan = "#7dcfff";
|
||||
blue = "#7aa2f7";
|
||||
purple = "#bb9af7";
|
||||
brown = "#db4b4b";
|
||||
};
|
||||
|
||||
# Systemd service helpers
|
||||
service = {
|
||||
make = name: serviceConfig: {
|
||||
description = serviceConfig.description or "Omnixient ${name} service";
|
||||
wantedBy = serviceConfig.wantedBy or [ "multi-user.target" ];
|
||||
after = serviceConfig.after or [ "network.target" ];
|
||||
serviceConfig = {
|
||||
Type = serviceConfig.type or "simple";
|
||||
User = serviceConfig.user or cfg.user;
|
||||
Group = serviceConfig.group or "users";
|
||||
Restart = serviceConfig.restart or "on-failure";
|
||||
RestartSec = serviceConfig.restartSec or "5";
|
||||
}
|
||||
// (serviceConfig.serviceConfig or { });
|
||||
};
|
||||
|
||||
user = name: serviceConfig: {
|
||||
home-manager.users.${cfg.user}.systemd.user.services.${name} = {
|
||||
description = serviceConfig.description or "Omnixient ${name} user service";
|
||||
wantedBy = [ "default.target" ];
|
||||
after = [ "graphical-session.target" ];
|
||||
serviceConfig = {
|
||||
Type = serviceConfig.type or "simple";
|
||||
Restart = serviceConfig.restart or "on-failure";
|
||||
RestartSec = serviceConfig.restartSec or "5";
|
||||
}
|
||||
// (serviceConfig.serviceConfig or { });
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
in
|
||||
{
|
||||
# Expose helpers to all modules via config.omni.lib
|
||||
options.omni.lib = mkOption {
|
||||
type = types.attrs;
|
||||
internal = true;
|
||||
description = "Omnixient shared helper functions (internal)";
|
||||
};
|
||||
|
||||
config = {
|
||||
omni.lib = helpers;
|
||||
|
||||
# Ensure required directories exist
|
||||
systemd.tmpfiles.rules = [
|
||||
"d ${helpers.paths.logs} 0755 root root -"
|
||||
"d ${helpers.paths.cache} 0755 root root -"
|
||||
"d ${helpers.paths.runtime} 0755 root root -"
|
||||
"d ${helpers.userPath ".local/bin"} 0755 ${cfg.user} users -"
|
||||
"d ${helpers.userPath ".local/share/omni"} 0755 ${cfg.user} users -"
|
||||
];
|
||||
|
||||
environment.variables = {
|
||||
OMNI_USER = cfg.user;
|
||||
OMNI_THEME = cfg.theme;
|
||||
OMNI_PRESET = cfg.preset or "custom";
|
||||
OMNI_CONFIG_DIR = helpers.paths.config;
|
||||
OMNI_CACHE_DIR = helpers.paths.cache;
|
||||
};
|
||||
|
||||
programs.bash.shellAliases = {
|
||||
# omni-rebuild is a script (packages/scripts.nix), not an alias
|
||||
omni-build = "sudo nixos-rebuild build --flake ${helpers.paths.config}#omni";
|
||||
omni-test = "sudo nixos-rebuild test --flake ${helpers.paths.config}#omni";
|
||||
omni-update = "cd ${helpers.paths.config} && sudo nix flake update";
|
||||
omni-clean = "sudo nix-collect-garbage -d && nix-collect-garbage -d";
|
||||
omni-generations = "sudo nix-env --list-generations --profile /nix/var/nix/profiles/system";
|
||||
omni-status = "omni-info";
|
||||
omni-features = "echo 'Enabled features:' && omni-info | grep -A 10 'Active Features'";
|
||||
omni-config = "cd ${helpers.paths.config}";
|
||||
omni-logs = "cd ${helpers.paths.logs}";
|
||||
};
|
||||
|
||||
environment.systemPackages = [
|
||||
(helpers.makeScript "omni-lib-test" "Test Omnixient library functions" ''
|
||||
echo "Omnixient Library Test"
|
||||
echo "===================="
|
||||
echo ""
|
||||
echo "Configuration:"
|
||||
echo " User: ${cfg.user}"
|
||||
echo " Theme: ${cfg.theme}"
|
||||
echo " Preset: ${cfg.preset or "none"}"
|
||||
echo ""
|
||||
echo "Colors (base16 scheme):"
|
||||
echo " Background: ${helpers.colors.bg}"
|
||||
echo " Foreground: ${helpers.colors.fg}"
|
||||
echo " Primary: ${helpers.colors.blue}"
|
||||
echo " Success: ${helpers.colors.green}"
|
||||
echo " Warning: ${helpers.colors.yellow}"
|
||||
echo " Error: ${helpers.colors.red}"
|
||||
echo ""
|
||||
echo "Paths:"
|
||||
echo " Config: ${helpers.paths.config}"
|
||||
echo " Logs: ${helpers.paths.logs}"
|
||||
echo " Cache: ${helpers.paths.cache}"
|
||||
echo " User home: ${helpers.userPath ""}"
|
||||
echo ""
|
||||
echo "Features:"
|
||||
${concatStringsSep "\n" (
|
||||
mapAttrsToList (
|
||||
name: enabled: ''echo " ${name}: ${if enabled then "enabled" else "disabled"}"''
|
||||
) cfg.features
|
||||
)}
|
||||
'')
|
||||
];
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue