-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.nix
More file actions
165 lines (148 loc) · 4.49 KB
/
lib.nix
File metadata and controls
165 lines (148 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
{
nixpkgs,
nix-darwin,
home-manager,
}@inputs:
let
inherit (nixpkgs) lib;
in
rec {
inherit (lib) optional optionals optionalAttrs;
/*
Check if a hostPlatform.system is Darwin.
Example:
lib.isDarwin "x86_64-darwin"
=> true
*/
isDarwin = system: lib.hasSuffix "darwin" system;
/*
Generate a NixOS/nix-darwin configuration based on a profile, with optional home-manager support.
A common configuration (referred to as a "profile") is used to share code between flakes.
This is used to avoid code repetition for flakes that configure multiple machines.
Example:
lib.createSystem
{
# The profile, usually defined elsewhere
modules = [ ./modules/graphical.nix ];
home-manager = {
enable = true;
modules = [ ./home-manager/modules/zsh.nix ];
};
}
# System definition, usually configured in the flake output
{ system = "x86_64-linux"; }
=> {
_module = { ... };
config = { ... };
extendModules = «lambda»;
extraArgs = { ... };
options = { ... };
pkgs = { ... };
type = { ... };
}
In a flake:
profile = { home-manager = { enable = true; modules = [ ./home-manager/modules/zsh.nix ]; }; };
nixosConfigurations.nixos-machine = lib.createSystem profile { system = "x86_64-linux"; };
darwinConfigurations.darwin-machine = lib.createSystem profile { system = "x86_64-darwin"; };
*/
createSystem =
profile:
{
system,
modules ? [ ],
extraConfig ? { },
home-manager ? { },
specialArgs ? { },
commonSpecialArgs ? { },
...
}:
let
_username = lib.optionalString _home-manager.enable (
home-manager.username or profile.home-manager.username
);
_modules = modules ++ (profile.modules or [ ]);
_extraConfig = lib.toList (lib.mergeAttrs (profile.extraConfig or { }) extraConfig);
_specialArgs = lib.mergeAttrs specialArgs (profile.specialArgs or { });
_homeManagerSpecialArgs = lib.mergeAttrs (home-manager.specialArgs or { }) (
profile.home-manager.specialArgs or { }
);
_commonSpecialArgs = lib.mergeAttrs commonSpecialArgs (profile.commonSpecialArgs or { });
_home-manager =
let
__extraConfig = lib.toList (
lib.mergeAttrs (profile.home-manager.extraConfig or { }) (home-manager.extraConfig or { })
);
in
{
enable =
if ((profile.home-manager.enable or false) || (home-manager.enable or false)) then true else false;
modules = (profile.home-manager.modules or [ ]) ++ (home-manager.modules or [ ]) ++ __extraConfig;
};
systemFunc = if (isDarwin system) then nix-darwin.lib.darwinSystem else nixpkgs.lib.nixosSystem;
homeManagerFunc =
if (isDarwin system) then
inputs.home-manager.darwinModules.home-manager
else
inputs.home-manager.nixosModules.home-manager;
in
systemFunc {
inherit system;
specialArgs = {
inherit system;
}
// _commonSpecialArgs
// _specialArgs;
modules =
lib.optionals _home-manager.enable [
homeManagerFunc
{
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {
inherit system;
}
// _commonSpecialArgs
// _homeManagerSpecialArgs;
home-manager.sharedModules = _home-manager.modules;
home-manager.users.${_username}.imports = _home-manager.modules;
}
]
++ _modules
++ _extraConfig;
};
vim = {
/*
Import a luafile inside of a vimrc.
Example:
lib.vim.mkLuaFile "foo.lua"
=> "lua << EOF dofile(\"foo.lua\") EOF"
*/
mkLuaFile = file: ''
lua << EOF
dofile("${file}")
EOF
'';
/*
Generate a lua section for a vimrc file.
Example:
lib.vim.mkLua "print('hello world')"
=> "lua << EOF print('hello world') EOF"
*/
mkLua = lua: ''
lua << EOF
${lua}
EOF
'';
};
# Generate keybindings for readline that apply to all modes: vi-command vi-insert and emacs.
readlineBindingsAllModes = bindings: ''
$if mode=vi
set keymap vi-command
${bindings}
set keymap vi-insert
${bindings}
$else
${bindings}
$endif
'';
}