Skip to content

Commit 689cb4e

Browse files
committed
lib: consolidate common functions between helpers
1 parent 654b018 commit 689cb4e

7 files changed

Lines changed: 183 additions & 196 deletions

File tree

lib/common.nix

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
{ inputs }:
2+
let
3+
inherit (inputs.nixpkgs.lib) filterAttrs mapAttrs';
4+
in
5+
{
6+
mkExtendedLib = flake: nixpkgs: nixpkgs.lib.extend flake.lib.overlay;
7+
8+
mkNixpkgsConfig = flake: {
9+
overlays = builtins.attrValues flake.overlays;
10+
config = {
11+
allowUnfree = true;
12+
permittedInsecurePackages = [
13+
# NOTE: citrix
14+
"libxml2-2.13.8"
15+
# NOTE: needed by emulationstation
16+
"freeimage-3.18.0-unstable-2024-04-18"
17+
# dev shells
18+
"aspnetcore-runtime-6.0.36"
19+
"aspnetcore-runtime-7.0.20"
20+
"aspnetcore-runtime-wrapped-7.0.20"
21+
"aspnetcore-runtime-wrapped-6.0.36"
22+
"dotnet-combined"
23+
];
24+
};
25+
};
26+
27+
mkHomeConfigs =
28+
{
29+
flake,
30+
system,
31+
hostname,
32+
}:
33+
let
34+
inherit (flake.lib.file) scanHomes;
35+
homesPath = ../homes;
36+
allHomes = scanHomes homesPath;
37+
in
38+
filterAttrs (
39+
_name: homeConfig: homeConfig.system == system && homeConfig.hostname == hostname
40+
) allHomes;
41+
42+
mkHomeManagerConfig =
43+
{
44+
extendedLib,
45+
inputs,
46+
system,
47+
matchingHomes,
48+
isNixOS ? true,
49+
}:
50+
if matchingHomes != { } then
51+
{
52+
home-manager = {
53+
useGlobalPkgs = true;
54+
useUserPackages = true;
55+
extraSpecialArgs = {
56+
inherit inputs system;
57+
inherit (inputs) self;
58+
lib = extendedLib;
59+
flake-parts-lib = inputs.flake-parts.lib;
60+
};
61+
sharedModules =
62+
[
63+
{ _module.args.lib = extendedLib; }
64+
]
65+
++ (
66+
if isNixOS then
67+
[
68+
inputs.home-manager.flakeModules.home-manager
69+
]
70+
else
71+
[ ]
72+
)
73+
++ [
74+
inputs.catppuccin.homeModules.catppuccin
75+
inputs.hypr-socket-watch.homeManagerModules.default
76+
inputs.nix-index-database.homeModules.nix-index
77+
inputs.sops-nix.homeManagerModules.sops
78+
../modules/home
79+
];
80+
users = mapAttrs' (_name: homeConfig: {
81+
name = homeConfig.username;
82+
value =
83+
{
84+
imports = [ homeConfig.path ];
85+
home = {
86+
inherit (homeConfig) username;
87+
homeDirectory = inputs.nixpkgs.lib.mkDefault (
88+
if isNixOS then "/home/${homeConfig.username}" else "/Users/${homeConfig.username}"
89+
);
90+
};
91+
}
92+
// (
93+
if isNixOS then
94+
{
95+
_module.args.username = homeConfig.username;
96+
}
97+
else
98+
{ }
99+
);
100+
}) matchingHomes;
101+
};
102+
}
103+
else
104+
{ };
105+
106+
mkSpecialArgs =
107+
{
108+
inputs,
109+
hostname,
110+
username,
111+
extendedLib,
112+
}:
113+
{
114+
inherit inputs hostname username;
115+
inherit (inputs) self;
116+
lib = extendedLib;
117+
flake-parts-lib = inputs.flake-parts.lib;
118+
format = "system";
119+
host = hostname;
120+
};
121+
}

lib/default.nix

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
{ inputs, ... }:
22
{
3-
# Extended library functions
43
flake.lib = {
5-
# Utility functions for the flake
64
mkSystem = import ./mk-system.nix { inherit inputs; };
75
mkHome = import ./mk-home.nix { inherit inputs; };
86
mkDarwin = import ./mk-darwin.nix { inherit inputs; };
9-
10-
# File utilities
7+
common = import ./common.nix { inherit inputs; };
118
file = import ./file {
129
inherit inputs;
1310
self = ../.;
1411
};
15-
16-
# Module utilities
1712
module = import ./module { inherit inputs; };
18-
19-
# Theme utilities
2013
theme = import ./theme { inherit inputs; };
21-
22-
# Base64 utilities
2314
base64 = import ./base64 { inherit inputs; };
24-
25-
# Library overlay for extending nixpkgs lib
2615
overlay = import ./overlay.nix { inherit inputs; };
2716
};
2817
}

lib/mk-darwin.nix

Lines changed: 26 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,89 +7,47 @@
77
...
88
}:
99
let
10-
# Import the khaneliparts flake to access the overlay
1110
flake = inputs.self or (throw "mkDarwin requires 'inputs.self' to be passed");
11+
common = import ./common.nix { inherit inputs; };
1212

13-
# Extend nixpkgs lib with khanelinix functions
14-
extendedLib = inputs.nixpkgs-unstable.lib.extend flake.lib.overlay;
15-
16-
# Auto-discover home configurations for this system+hostname
17-
inherit (flake.lib.file) scanHomes;
18-
homesPath = ../homes;
19-
allHomes = scanHomes homesPath;
20-
21-
# Filter for matching system and hostname
22-
matchingHomes = extendedLib.filterAttrs (
23-
_name: homeConfig: homeConfig.system == system && homeConfig.hostname == hostname
24-
) allHomes;
25-
26-
# Generate home-manager configuration for matching users
27-
homeManagerConfig =
28-
if matchingHomes != { } then
29-
{
30-
home-manager = {
31-
useGlobalPkgs = true;
32-
useUserPackages = true;
33-
extraSpecialArgs = {
34-
inherit inputs system;
35-
inherit (flake) self;
36-
lib = extendedLib;
37-
flake-parts-lib = inputs.flake-parts.lib;
38-
};
39-
sharedModules = [
40-
# Provide extended lib to all modules
41-
{ _module.args.lib = extendedLib; }
42-
43-
inputs.catppuccin.homeModules.catppuccin
44-
inputs.hypr-socket-watch.homeManagerModules.default
45-
inputs.nix-index-database.homeModules.nix-index
46-
inputs.sops-nix.homeManagerModules.sops
47-
../modules/home
48-
];
49-
users = extendedLib.mapAttrs' (_name: homeConfig: {
50-
name = homeConfig.username;
51-
value = {
52-
imports = [ homeConfig.path ];
53-
home = {
54-
inherit (homeConfig) username;
55-
homeDirectory = inputs.nixpkgs-unstable.lib.mkDefault "/Users/${homeConfig.username}";
56-
};
57-
};
58-
}) matchingHomes;
59-
};
60-
}
61-
else
62-
{ };
13+
extendedLib = common.mkExtendedLib flake inputs.nixpkgs;
14+
matchingHomes = common.mkHomeConfigs {
15+
inherit
16+
flake
17+
system
18+
hostname
19+
;
20+
};
21+
homeManagerConfig = common.mkHomeManagerConfig {
22+
inherit
23+
extendedLib
24+
inputs
25+
system
26+
matchingHomes
27+
;
28+
isNixOS = false;
29+
};
6330
in
6431
inputs.darwin.lib.darwinSystem {
6532
inherit system;
6633

67-
specialArgs = {
68-
inherit inputs hostname username;
69-
inherit (inputs) self;
70-
lib = extendedLib;
71-
flake-parts-lib = inputs.flake-parts.lib;
72-
format = "system";
73-
host = hostname;
34+
specialArgs = common.mkSpecialArgs {
35+
inherit
36+
inputs
37+
hostname
38+
username
39+
extendedLib
40+
;
7441
};
7542

7643
modules = [
77-
# Provide extended lib to all modules
7844
{ _module.args.lib = extendedLib; }
7945

8046
# Configure nixpkgs with overlays
8147
{
8248
nixpkgs = {
8349
inherit system;
84-
overlays = builtins.attrValues flake.overlays;
85-
config = {
86-
allowUnfree = true;
87-
permittedInsecurePackages = [
88-
# NOTE: citrix
89-
"libxml2-2.13.8"
90-
];
91-
};
92-
};
50+
} // common.mkNixpkgsConfig flake;
9351
}
9452

9553
inputs.home-manager.darwinModules.home-manager
@@ -100,12 +58,7 @@ inputs.darwin.lib.darwinSystem {
10058
# Auto-inject home configurations for this system+hostname
10159
homeManagerConfig
10260

103-
# Base Darwin modules
10461
../modules/darwin
105-
106-
# Host-specific configuration
10762
../systems/${system}/${hostname}
108-
109-
# Additional modules passed as arguments
11063
] ++ modules;
11164
}

lib/mk-home.nix

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,17 @@
77
...
88
}:
99
let
10-
# Import the khaneliparts flake to access the overlay
1110
flake = inputs.self or (throw "mkHome requires 'inputs.self' to be passed");
11+
common = import ./common.nix { inherit inputs; };
1212

13-
# Extend nixpkgs lib with khanelinix functions
14-
extendedLib = inputs.nixpkgs-unstable.lib.extend flake.lib.overlay;
13+
extendedLib = common.mkExtendedLib flake inputs.nixpkgs;
1514
in
1615
inputs.home-manager.lib.homeManagerConfiguration {
17-
pkgs = import inputs.nixpkgs-unstable {
18-
inherit system;
19-
overlays = builtins.attrValues flake.overlays;
20-
config = {
21-
allowUnfree = true;
22-
# TODO: cleanup when available
23-
permittedInsecurePackages = [
24-
# NOTE: citrix
25-
"libxml2-2.13.8"
26-
# NOTE: needed by emulationstation
27-
"freeimage-3.18.0-unstable-2024-04-18"
28-
# dev shells
29-
"aspnetcore-runtime-6.0.36"
30-
"aspnetcore-runtime-7.0.20"
31-
"aspnetcore-runtime-wrapped-7.0.20"
32-
"aspnetcore-runtime-wrapped-6.0.36"
33-
"dotnet-combined"
34-
];
35-
};
36-
};
16+
pkgs =
17+
import inputs.nixpkgs {
18+
inherit system;
19+
}
20+
// common.mkNixpkgsConfig flake;
3721

3822
extraSpecialArgs = {
3923
inherit
@@ -48,18 +32,13 @@ inputs.home-manager.lib.homeManagerConfiguration {
4832
};
4933

5034
modules = [
51-
# Provide extended lib to all modules
5235
{ _module.args.lib = extendedLib; }
5336

54-
# Third-party home modules
5537
inputs.catppuccin.homeModules.catppuccin
5638
inputs.hypr-socket-watch.homeManagerModules.default
5739
inputs.nix-index-database.homeModules.nix-index
5840
inputs.sops-nix.homeManagerModules.sops
5941

60-
# Base home modules
6142
../modules/home
62-
63-
# Additional modules passed as arguments
6443
] ++ modules;
6544
}

0 commit comments

Comments
 (0)