-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
160 lines (137 loc) · 5.83 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
{
inputs.nix.url = "github:nixos/nix";
inputs.nixpkgs.url = "nixpkgs/nixos-21.05-small";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.arxsrc = {
url = "github:solidsnack/arx";
flake = false;
};
outputs = { self, nix, nixpkgs, flake-utils, arxsrc, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
patchedNix = nix.packages."${system}".nix-static.overrideAttrs (_: {
patches = [ ./ca-cert-path.patch ./relocatable.patch ./cross-fingers.patch ./fixed-output.patch ];
doCheck = false;
});
in
{
packages =
let
# Location of srun & sallocate binaries on the HPC system
SLURMPREFIX = "/usr/bin/";
# Location of nix store
# Must be shared with build nodes
STOREROOT = "/stornext/HPCScratch/$USER";
# Location of temporary files (e.g., build directories)
# Must be shared with build nodes
TMPDIR = "${STOREROOT}/tmp";
# Use proot instead of bubblewrap
useProot = false;
SRUN = "${SLURMPREFIX}/srun";
SALLOC = "${SLURMPREFIX}/salloc";
makeWrapper = name: minimal: script: pkgs.writeScript name ''
#!/bin/sh
set -e
set -o pipefail
function dirof {
SRC="$1"
BASE=''${SRC##*/}
DIR=''${SRC%"$BASE"}
}
dirof "$0"
SCRIPT_DIR="$( cd -- "$DIR" &> /dev/null && pwd -P)"
${pkgs.lib.optionalString (!minimal) ''
dirof "$SCRIPT_DIR"
LIBEXEC="''${DIR}libexec/nix"
export TMPDIR="${TMPDIR}"
export NIX_STOREROOT="${STOREROOT}"
export NIX_SRUN="${SRUN}"
export NIX_SALLOC="${SALLOC}"
command -v mkdir &> /dev/null && mkdir -p "$TMPDIR"
command -v mkdir &> /dev/null && mkdir -p "$NIX_STOREROOT"/nix
''}
${script}
'';
slurmNix = patchedNix.overrideAttrs (attrs: {
patches = attrs.patches ++ [ ./slurm-submit.patch ];
});
arx' = pkgs.haskellPackages.arx.overrideAttrs
(_: {
version = "git";
src = arxsrc;
preConfigure = ''
echo "git" > version
'';
patches = [ ./arx-cwd.patch ];
});
chroot-wrapper = makeWrapper "chroot-wrapper" false ''
exec $LIBEXEC/nix-user-chroot "$NIX_STOREROOT/nix" "$@"
'';
ssh-wrapper = makeWrapper "ssh-wrapper" false ''
exec $LIBEXEC/nix-user-chroot "$NIX_STOREROOT/nix" $LIBEXEC/bash -c 'exec '"$SCRIPT_DIR"'/$SSH_ORIGINAL_COMMAND'
'';
nix-wrapper = makeWrapper "nix-wrapper" false ''
exec $LIBEXEC/nix-user-chroot "$NIX_STOREROOT/nix" $SCRIPT_DIR/nix \
--experimental-features 'nix-command flakes' \
--option sandbox false "$@"
'';
proot-wrapper = makeWrapper "proot-wrapper" true ''
ROOT="$1"
shift
exec $SCRIPT_DIR/proot -b "$ROOT":/nix/ "$@"
'';
bwrap-wrapper = makeWrapper "bwrap-wrapper" true ''
ROOT="$1"
shift
rootArgs=""
for path in /* ; do
if [ "$path" != "/nix" ] && [ "$path" != "/dev" ] ; then
rootArgs="$rootArgs --bind $path $path"
fi
done
exec $SCRIPT_DIR/bwrap --bind "$ROOT" /nix/ $rootArgs --dev-bind /dev /dev "$@"
'';
proot = pkgs.fetchurl {
url = "https://github.com/proot-me/proot/releases/download/v5.2.0/proot-v5.2.0-x86_64-static";
sha256 = "sha256-6wZDs8SnfGsoe8Ev0WrXqUhs0vPE72Oh7oyn74pK4vA=";
};
bundler = what:
pkgs.runCommand "build-bundle" { } ''
${arx'}/bin/arx tmpx --tmpdir '${TMPDIR}' ${tarball} // ${what} > $out
chmod 755 $out
'';
tarball =
pkgs.runCommand "slurm-nix.tar.bz2" { } ''
install -Dm 755 ${slurmNix}/bin/nix out/bin/nix
install -Dm 755 ${pkgs.pkgsStatic.bash}/bin/bash out/libexec/nix/bash
install -Dm 755 ${ssh-wrapper} out/bin/ssh-wrapper
install -Dm 755 ${nix-wrapper} out/bin/nix-wrapper
install -Dm 755 ${chroot-wrapper} out/bin/chroot-wrapper
ln -s ../../bin/nix out/libexec/nix/build-remote
${if useProot then ''
install -Dm 755 ${proot} out/libexec/nix/proot
install -Dm 755 ${proot-wrapper} out/libexec/nix/nix-user-chroot
'' else ''
install -Dm 755 ${pkgs.pkgsStatic.bubblewrap}/bin/bwrap out/libexec/nix/bwrap
install -Dm 755 ${bwrap-wrapper} out/libexec/nix/nix-user-chroot
''}
for cmd in build channel collect-garbage copy-closure daemon env hash instantiate prefetch-url shell store ; do
ln -s ./nix out/bin/nix-$cmd
done
tar -Jcvf $out -C ./out .
'';
in
{
slurm = tarball;
slurm-ssh-wrapper-bundle = bundler "ssh-wrapper";
slurm-nix-wrapper-bundle = bundler "nix-wrapper";
};
defaultPackage =
pkgs.runCommand "build-package" { } ''
install -D ${patchedNix}/bin/nix $out/bin/nix
install -D ${pkgs.pkgsStatic.bash}/bin/bash $out/libexec/nix/bash
ln -s ../../bin/nix $out/libexec/nix/build-remote
'';
});
}