-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.nix
219 lines (217 loc) · 7.16 KB
/
default.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
# SPDX-FileCopyrightText: © 2024 m-click.aero GmbH <https://m-click.aero>
# SPDX-License-Identifier: Apache-2.0
{
pkgs ? import (builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/24.05.tar.gz";
sha256 = "1lr1h35prqkd1mkmzriwlpvxcb34kmhc9dnr48gkm8hh089hifmx";
}) { config = {}; overlays = []; }
}:
let
staticBubblewrap = pkgs.pkgsStatic.bubblewrap;
hosts =
pkgs.writeText "hosts" ''
127.0.0.1 localhost
::1 localhost
'';
cacert = pkgs.cacert;
bash = pkgs.bash;
bintools = pkgs.bintools;
coreutils = pkgs.coreutils;
findutils = pkgs.findutils;
fontconfigOut = pkgs.fontconfig.out;
fswatch = pkgs.fswatch;
getClosure = packages:
pkgs.lib.lists.remove "" (
pkgs.lib.strings.splitString "\n" (
builtins.readFile (pkgs.writeClosure packages)
)
);
symlinkJoinSubdirs = packages: subdir:
pkgs.symlinkJoin {
name = subdir;
paths = builtins.filter pkgs.lib.filesystem.pathIsDirectory (
builtins.map
(package: "${package}/${subdir}")
packages
);
};
mkPortableEnv = { packages }:
let
packagesClosure = getClosure packages;
binDir = symlinkJoinSubdirs packages "bin";
emacsDir = symlinkJoinSubdirs packagesClosure "share/emacs";
emacsVersionLispDir = pkgs.lib.lists.findSingle
pkgs.lib.filesystem.pathIsDirectory
""
"error-multiple-emacs-versions"
(pkgs.lib.mapAttrsToList
(name: type: "${emacsDir}/${name}/lisp")
(builtins.readDir emacsDir)
);
emacsSiteLispDir =
if pkgs.lib.filesystem.pathIsDirectory "${emacsDir}/site-lisp" then
"${emacsDir}/site-lisp"
else
"";
emacsLoadPathOrEmpty =
builtins.concatStringsSep
":"
(
builtins.filter
(pathEntry: pathEntry != "")
[
emacsVersionLispDir
emacsSiteLispDir
]
);
fontsDir = symlinkJoinSubdirs packagesClosure "share/fonts";
fontsConf = pkgs.writeText "fonts.conf" (
if builtins.readDir fontsDir == {} then
""
else
''
<fontconfig>
<description>Environment configuration file</description>
<dir>${fontsDir}</dir>
<include>${fontconfigOut}/etc/fonts/fonts.conf</include>
</fontconfig>
''
);
libDir = symlinkJoinSubdirs packagesClosure "lib";
ocamlSiteLibDirOrEmpty = pkgs.lib.lists.findSingle
pkgs.lib.filesystem.pathIsDirectory
""
"error-multiple-ocaml-versions"
(pkgs.lib.mapAttrsToList
(name: type: "${libDir}/ocaml/${name}/site-lib")
(if pkgs.lib.filesystem.pathIsDirectory "${libDir}/ocaml" then builtins.readDir "${libDir}/ocaml" else {})
);
in
pkgs.writeScript "env" ''
#!/bin/sh
set -eu
if [ "$#" = 0 ]; then
echo "Usage: $0 COMMAND ARGS..." >&2
exit 1
fi
basedir=$(realpath -- "$(dirname -- "$(realpath -- "$0")")/../..")
mkdir -p -- "$basedir/homedir"
mkdir -p -- "$basedir/tmp"
exec "$basedir${staticBubblewrap}/bin/bwrap" \
--unshare-all \
--share-net \
--clearenv \
--die-with-parent \
--dev /dev \
--proc /proc \
--bind "$basedir/nix" /nix \
--bind "$basedir/homedir" /homedir \
--bind "$basedir/tmp" /tmp \
--symlink usr/bin /bin \
--symlink ${binDir} /usr/bin \
--symlink ${hosts} /etc/hosts \
--ro-bind-try /etc/resolv.conf /etc/resolv.conf \
--bind . "$(pwd)" \
--remount-ro / \
--setenv DISPLAY "''${DISPLAY:-}" \
--setenv EMACSLOADPATH '${emacsLoadPathOrEmpty}' \
--setenv FONTCONFIG_FILE ${fontsConf} \
--setenv HOME /homedir \
--setenv LIBRARY_PATH ${libDir} \
--setenv NIXPKGS_ALLOW_INSECURE "''${NIXPKGS_ALLOW_INSECURE:-0}" \
--setenv NIX_SSL_CERT_FILE ${cacert}/etc/ssl/certs/ca-bundle.crt \
--setenv OCAMLPATH '${ocamlSiteLibDirOrEmpty}' \
--setenv PATH /usr/bin \
--setenv TERM "''${TERM:-}" \
-- \
${bash}/bin/sh -euc '
case "$(${coreutils}/bin/basename -- "$2")" in
nix|nix-*)
echo "$0: [getanix] Recognized Nix command, enabling convenience features." >&2
if [ -e /nix/var/nix/db-refs ]; then
echo "$0: [getanix] Initializing Nix database ..." >&2
if [ -e /nix/var/nix/db ]; then
echo "Error: Nix database already exists." >&2
exit 1
fi
"$(${coreutils}/bin/dirname -- "$(command -v "$2")")/nix-store" --register-validity </nix/var/nix/db-refs
${coreutils}/bin/rm -f /nix/var/nix/db-refs
echo "$0: [getanix] Nix database successfully initialized." >&2
fi
${fswatch}/bin/fswatch -l 0.1 -m inotify_monitor -0 --event MovedTo . \
| ${findutils}/bin/xargs -0 -I {} -- ${bash}/bin/sh -euc '"'"'
if [ -s "$2" ] && [ "$(${coreutils}/bin/readlink -- "$2" | ${coreutils}/bin/head -c 1)" = / ]; then
echo "$0: [getanix] Adjusting symlink: $2" >&2
${coreutils}/bin/ln -rsfT -- "$1$(${coreutils}/bin/readlink -- "$2")" "$2"
fi
'"'"' "$0" "$1" {} &
;;
*) ;;
esac
shift
exec "$@"
' \
"$0" \
"$basedir" \
"$@"
'';
mkPortableEnvTarCompressed = { env, compressionCommand, suffix, nativeBuildInputs }:
let
closure = pkgs.writeClosure [ env ];
in
pkgs.runCommand
"env.${suffix}"
{
inherit nativeBuildInputs;
exportReferencesGraph = [ "refs" env ];
}
''
mkdir -p tmp/.envroot/nix/store
cat ${closure} | while read dep; do
cp -a $dep tmp/.envroot/nix/store/
done
mkdir -p tmp/.envroot/nix/var/nix
cat refs >tmp/.envroot/nix/var/nix/db-refs
ln -sf .envroot${env} tmp/env
./tmp/env nix-store --optimise
chmod -R u+w tmp/.envroot/nix/store/.links
rm -rf tmp/.envroot/nix/store/.links
mkdir -p .envroot/nix
mv tmp/.envroot/nix/store .envroot/nix/
mv tmp/env ./
mkdir -p .envroot/nix/var/nix
cat refs >.envroot/nix/var/nix/db-refs
tar c --sort=name --owner 0 --group 0 --numeric-owner --mtime=@1 -- .envroot env | ${compressionCommand} >$out
'';
mkPortableEnvTgz = { env }:
mkPortableEnvTarCompressed {
inherit env;
compressionCommand = "gzip -9nv";
suffix = "tgz";
nativeBuildInputs = [];
};
mkPortableEnvTarZst = { env }:
mkPortableEnvTarCompressed {
inherit env;
compressionCommand = "zstd --ultra -22v";
suffix = "tar.zst";
nativeBuildInputs = [
pkgs.zstd
];
};
bootstrapEnvTgz = mkPortableEnvTgz {
env = mkPortableEnv {
packages = [
pkgs.nix
];
};
};
in
{
inherit
bootstrapEnvTgz
mkPortableEnv
mkPortableEnvTgz
mkPortableEnvTarZst
;
}