-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
218 lines (198 loc) · 5.1 KB
/
flake.nix
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
{
inputs = {
systems.url = "github:nix-systems/default";
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
rust.url = "github:oxalica/rust-overlay";
};
outputs =
{
self,
nixpkgs,
systems,
rust,
}:
let
inherit (builtins)
attrNames
dirOf
elem
filter
head
isList
map
pathExists
readDir
tail
toPath
;
pathOf =
{
base ? ./.,
direction ? "both",
items,
}:
let
#| Ensure the base is a valid path
basePath = toPath base;
#| Ensure items is a list
itemList = if isList items then items else [ items ];
#| Function to find the first existing item in the directory
findItem =
dir:
let
existingItems = filter (item: pathExists (dir + ("/" + item))) itemList;
in
if existingItems == [ ] then null else dir + ("/" + head existingItems);
#| Function to get parent directory
dirAbove =
dir:
let
parent = dirOf dir;
in
if parent == dir then null else parent;
#| Function to get subdirectories
dirBelow =
dir:
let
dirContents = readDir dir;
in
map (name: dir + ("/" + name)) (
filter (name: dirContents.${name} == "directory") (attrNames dirContents)
);
#| Generic search function
search =
dir: getNext:
let
foundItem = findItem dir;
in
if foundItem != null then
foundItem
else
let
next = getNext dir;
in
if next == null then
null
else if isList next then
searchList next getNext
else
search next getNext;
#| Helper function to search a list of directories
searchList =
dirs: getNext:
if dirs == [ ] then
null
else
let
result = search (head dirs) getNext;
in
if result != null then result else searchList (tail dirs) getNext;
# Function to get next directory based on search direction
getNext = if direction == "up" then dirAbove else dirBelow;
in
assert
elem direction [
"up"
"down"
"both"
]
|| throw "Invalid direction: must be 'up' or 'down'";
# If direction is not specified, search in both directions
if direction == "both" then
let
downSearch = search basePath dirBelow;
upSearch = search basePath dirAbove;
in
if downSearch != null then downSearch else upSearch
else
search basePath getNext;
toolchainPath = pathOf {
items = [
"toolchain.toml"
"toolchain"
"rust-toolchain"
"rust-toolchain.toml"
];
};
configPath = dirOf toolchainPath;
binPath = dirOf (pathOf {
items = [
"init"
"init.sh"
"init.fish"
"init.zsh"
];
});
perSystem =
f:
nixpkgs.lib.genAttrs (import systems) (
system:
f {
pkgs = import nixpkgs {
inherit system;
overlays = [
(import rust)
(self: super: { toolchain = super.rust-bin.fromRustupToolchainFile toolchainPath; })
];
};
}
);
in
{
devShells = perSystem (
{ pkgs }:
{
default = pkgs.mkShell {
packages = with pkgs; [
# Dependencies
openssl
pkg-config
# Core
toolchain
cargo-watch
cargo-edit
cargo-generate
# Utilities
bat
direnv
dust
eza
fd
helix
just
pls
ripgrep
tokei
trashy
treefmt
starship
fish
# Formatters
mdformat
nodePackages.prettier
shellcheck
shfmt
taplo
yamlfmt
nixfmt-rfc-style
# Shells
fish
nushell
zsh
];
shellHook = ''
[ -d "${binPath}" ] || {
printf "Error: Configuration path %s does not exist." ${binPath}
return 1
}
[ -f "${binPath}/init" ] || {
printf "Error: init not found in %s." ${binPath}
return 1
}
. "${binPath}/init"
'';
};
}
);
};
}