-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
238 lines (210 loc) · 7.38 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
{
description = "Nuenv: a Nushell environment for Nix";
inputs = {
nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2305.*.tar.gz"; # Provides Nushell v0.81.0
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, rust-overlay }:
let
supportedSystems = [
"x86_64-linux" # 64-bit Intel/AMD Linux
"aarch64-linux" # 64-bit ARM Linux
"x86_64-darwin" # 64-bit Intel macOS
"aarch64-darwin" # 64-bit ARM macOS
];
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
pkgs = import nixpkgs {
inherit system;
overlays = [
self.overlays.nuenv # Supply nixpkgs.nuenv.mkDerivation
rust-overlay.overlays.default
(final: prev: {
rustToolchain = prev.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
})
];
};
inherit system;
});
in
{
overlays = rec {
default = nuenv;
nuenv = final: prev: {
nuenv = {
mkDerivation = self.lib.mkNushellDerivation
# Provide Nushell package
prev.nushell
# Provide default system
prev.system;
writeScriptBin = self.lib.mkNushellScript
# Provide Nushell package
prev.nushell
# Provide helper function
prev.writeTextFile;
# TODO: mkShell
};
};
};
lib =
let internalLib = import ./lib/nuenv.nix;
in {
/*
mkNushellDerivation creates a nushell derivation builder
Type:
[package] -> [string] -> package
Example:
let nushellBuilder = mkNuShellDerivation
pkgs.nushell
pkgs.stdenv.hostPlatform.system;
*/
mkNushellDerivation =
# nushell package to use for derivation build environment
nushellPkg:
# system as a string
system:
internalLib.mkNushellDerivation nushellPkg system;
/*
mkNushellScript creates a nushell script builder
Type:
[string] -> [string] -> package
Example:
let mkNushellScript = mkNushellScript
pkgs.nushell
pkgs.writeTextFile;
let outScript = mkNushellScript
"repair-infra.nu"
''
print -e "(ansi red)fixing infrastructure(ansi reset)"
print "dont_crash_anymore=true" | save -a server_config.toml
'';
*/
mkNushellScript =
# nushell package to use for script shebang/execution
nushellPkg:
# function to use for writing out script (example: pkgs.writeTextFile)
writeTextFile:
internalLib.mkNushellScript nushellPkg writeTextFile;
};
devShells = forAllSystems ({ pkgs, system }: {
default = pkgs.mkShell {
packages = with pkgs; [ nushell ];
};
ci = pkgs.mkShell {
packages = with pkgs; [ cachix direnv nushell ];
};
# A dev environment with Nuenv's helper functions available
nuenv = pkgs.mkShell {
packages = with pkgs; [ nushell ];
shellHook = ''
nu --config ${./nuenv/user-env.nu}
'';
};
});
packages = forAllSystems ({ pkgs, system }: rec {
default = hello;
run-me = pkgs.nuenv.writeScriptBin {
name = "run-me";
script = ''
def color [color: string, msg: string] { $"(ansi $color)($msg)(ansi reset)" }
def info [msg: string] { print $"(color "blue" "INFO"): ($msg)" }
def success [msg: string] { print $"(color "green" $msg)" }
info $"Hello, NixCon (date now | date format "%Y")!"
info "So lovely to see everyone today"
info "Conference status:"
success "SUCCESS"
info "Nearing completion..."
info "Script status:"
success "DONE"
'';
};
nuenv-commands = pkgs.writeScriptBin "nuenv-commands" ''
${pkgs.nushell}/bin/nu --env-config ${./nuenv/user-env.nu} --commands "nuenvCommands"
'';
# For NixCon
hello = pkgs.nuenv.mkDerivation {
name = "hello-nix-nushell";
packages = [ pkgs.hello ];
src = ./.;
build = builtins.readFile ./example/hello.nu;
MESSAGE = "Hello from Nix + Bash";
};
# An example Nushell-based derivation
nixcon = pkgs.nuenv.mkDerivation {
name = "hello-nixcon";
packages = [ pkgs.hello ];
src = ./.;
build = builtins.readFile ./example/hello.nu;
MESSAGE = "NixCon ❄️! Servus, grüezi, hallo, und hallöchen 😄";
};
# The Nushell-based derivation above but with debug mode disabled
helloNoDebug = pkgs.nuenv.mkDerivation {
name = "hello-nix-nushell";
packages = [ pkgs.hello ];
src = ./.;
build = builtins.readFile ./example/hello.nu;
debug = false;
MESSAGE = "Hello from Nix + Bash, but no debugging this time";
};
# A non-overlay version
direct = self.lib.mkNushellDerivation pkgs.nushell system {
name = "no-overlay";
src = ./.;
build = ''
let share = $"($env.out)/share"
(version).version | save nushell-version.txt
mkdir $share
cp nushell-version.txt $share
'';
};
# Show that Nuenv works when drawing sources from GitHub
githubSrc = pkgs.nuenv.mkDerivation {
name = "advanced-nix-nushell";
src = pkgs.fetchFromGitHub {
owner = "DeterminateSystems";
repo = "nuenv";
rev = "c8adf22d9cdc61d4777ad4b9d193e9b22547419e";
sha256 = "sha256-fu4WTFHlceasWpi6zF0nlent4KSmPAdsiKTrGixDEiI=";
};
build = ''
let share = $"($env.out)/share"
mkdir $share
cp README.md $share
'';
};
# The same derivation above but using the stdenv
stdenv = pkgs.stdenv.mkDerivation {
name = "just-experimenting";
buildInputs = [ pkgs.hello ];
src = ./.;
buildPhase = ''
hello --greeting "''${MESSAGE}" > hello.txt
substituteInPlace hello.txt --replace "Bash" "Nushell"
'';
installPhase = ''
mkdir -p $out/share
cp hello.txt $out/share
'';
MESSAGE = "Hello from Nix + Bash";
};
# Simple that relies on the Nushell derivation
other = pkgs.stdenv.mkDerivation {
name = "other";
src = ./.;
installPhase = ''
ls ${self.packages.${system}.hello}
mkdir -p $out/share
cp ${self.packages.${system}.hello}/share/hello.txt $out/share/copied.txt
'';
};
# An Nushell-based derivation that errors
error = pkgs.nuenv.mkDerivation {
name = "throws-nushell-error";
src = ./.;
build = builtins.readFile ./example/error.nu;
};
});
};
}