-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.nix
More file actions
executable file
·142 lines (131 loc) · 4.08 KB
/
lib.nix
File metadata and controls
executable file
·142 lines (131 loc) · 4.08 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
crane: rec {
nushellWith =
{ pkgs, ... }@args:
let
nushellEnv = import ./nushell-with.nix crane args;
in
nushellEnv
// {
inherit (mkLib (pkgs // { nushell = nushellEnv; }))
runNuCommand
runNuScript
writeNuScriptBin
writeNushellApplication
;
};
# A nushell version of pkgs.runCommand
#
# Use a inlined Nu script to build a derivation.
# This script should write to $env.out
runNuCommand =
pkgs: name: bins: command:
pkgs.runCommand name bins ''
${pkgs.nushell}/bin/nu ${
pkgs.lib.escapeShellArgs [
"-c"
command
]
}
'';
# Call a Nu script, passing it arguments, to build a derivation.
# This script should write to $env.out
runNuScript =
pkgs: name: bins: scriptPath: args:
pkgs.runCommand name bins ''
${pkgs.nushell}/bin/nu ${scriptPath} ${pkgs.lib.escapeShellArgs args}
'';
# A nushell version of pkgs.writeScriptBin
#
# Build a derivation that executes the given inlined nushell script
writeNuScriptBin =
pkgs: name: contents:
pkgs.writeScriptBin name ''
#!${pkgs.lib.getExe pkgs.nushell}
${contents}
'';
# A nushell version of pkgs.writeShellApplication
#
# Build a derivation that executes the given inlined nushell script,
# and can add runtimeInputs that this script can use
writeNushellApplication =
pkgs: args:
pkgs.writeShellApplication (
args
// {
text = ''
#!${pkgs.lib.getExe pkgs.nushell}
${args.text}
'';
}
);
# Patch a nushell library so it refers to a specific PATH and can use its dependencies
makeNuLibrary =
{
# Nixpkgs imported:
pkgs,
# Name of the library:
name,
# Folder containing the library:
src,
# Nu libraries this lib depends on:
dependencies ? [ ],
# Binary dependencies (list of folders to add to the PATH):
path ? [ ],
}:
runNuScript pkgs name { } ../nu-src/patch-deps.nu [
src
(builtins.toJSON { inherit dependencies path; })
];
# Extract the build env of a derivation as a file
extractBuildEnv =
{
pkgs, # Nixpkgs imported
drv, # The derivation to override
preBuildHook ? "", # Bash code to set extra env vars, e.g. by sourcing a file
selected ? [ ".*" ], # Which env vars to keep (regexes)
rejected ? [ ], # After selection, which env vars to remove (regexes)
format ? "nuon", # A file extension. Which format to use for the env file
}:
let
toNuonList = list: "\"[${pkgs.lib.strings.escapeShellArgs list}]\"";
in
drv.overrideAttrs {
name = "${drv.name}-env.${format}";
buildCommand = ''
${preBuildHook}
${pkgs.nushell}/bin/nu -n ${../nu-src/extract-env.nu} \
-s ${toNuonList selected} -r ${toNuonList rejected} -o $out
'';
};
# Make a nushell module that, when imported with 'use' or 'overlay use',
# will add to the current env the contents of some env files (which can be
# any format usable by nushell 'open' command)
makeNuModuleExporting =
{
pkgs, # Nixpkgs imported
env-files, # The json/toml/yaml/nuon env files that the produced module should export
merge-strategy ? "prepend", # How list-like env vars (notably PATH) should be dealt with
}:
pkgs.writeText "env.nu" ''
use ${../nu-src/extract-env.nu} merge-into-env
export-env {
merge-into-env --strategy ${merge-strategy} [${pkgs.lib.strings.concatStringsSep " " env-files}]
}
'';
# Set pkgs once for all the above functions
mkLib =
pkgs:
let
withPkgs = f: args: f ({ inherit pkgs; } // args);
in
{
nushellWith = withPkgs nushellWith;
runNuCommand = runNuCommand pkgs;
runNuScript = runNuScript pkgs;
writeNuScriptBin = writeNuScriptBin pkgs;
writeNushellApplication = writeNushellApplication pkgs;
makeNuLibrary = withPkgs makeNuLibrary;
extractBuildEnv = withPkgs extractBuildEnv;
makeNuModuleExporting = withPkgs makeNuModuleExporting;
};
}