Skip to content

Commit fd53454

Browse files
committed
extract n2c args for nix support to function
1 parent 1273f38 commit fd53454

File tree

2 files changed

+87
-62
lines changed

2 files changed

+87
-62
lines changed

nix/cloud/library.nix

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,81 @@
44
}: {
55
ociNamer = oci: builtins.unsafeDiscardStringContext "${oci.imageName}:${oci.imageTag}";
66
pp = v: builtins.trace (builtins.toJSON v) v;
7+
8+
# Adds arguments that enable nix support inside the container.
9+
addN2cNixArgs =
10+
# This must include all dependencies used in the image to allow nix builds
11+
# in the container (Nix won't know about paths otherwise and fail on trying
12+
# to write read-only data).
13+
closureRootPaths:
14+
# The arguments to nix2container's buildImage or buildLayer function to enrich.
15+
args: let
16+
inherit (inputs.nixpkgs) lib cacert symlinkJoin closureInfo runCommandNoCC;
17+
18+
closure = closureInfo {
19+
rootPaths = {inherit cacert;} // closureRootPaths;
20+
};
21+
22+
global = runCommandNoCC "global" {} ''
23+
mkdir -p $out $out/etc
24+
cp ${closure}/registration $out
25+
26+
echo 'root:x:0:' > $out/etc/group
27+
echo 'nixbld:x:30000:nixbld1' > $out/etc/group
28+
29+
echo 'root:!:0:0::/local:/bin/bash' > $out/etc/passwd
30+
echo 'nixbld1:!:30001:30000:Nix build user 1:/var/empty:/bin/nologin' >> $out/etc/passwd
31+
'';
32+
33+
nixConf = runCommandNoCC "nix.conf" {} ''
34+
mkdir -p $out/etc/nix
35+
cat > $out/etc/nix/nix.conf <<'EOF'
36+
# If /dev/kvm does not actually exist in the container
37+
# we would rather build without KVM than fail.
38+
extra-system-features = kvm
39+
40+
experimental-features = nix-command flakes
41+
EOF
42+
'';
43+
44+
tmp = runCommandNoCC "tmp" {} ''
45+
mkdir -p $out/tmp
46+
'';
47+
in
48+
args
49+
// {
50+
config =
51+
args.config
52+
or {}
53+
// {
54+
Env = lib.mapAttrsToList (n: v: "${n}=${v}") {
55+
SSL_CERT_FILE = "/etc/ssl/certs/ca-bundle.crt";
56+
};
57+
};
58+
contents =
59+
args.contents
60+
or []
61+
++ [
62+
(symlinkJoin {
63+
name = "deps";
64+
paths = with inputs.nixpkgs; [
65+
cacert
66+
gitMinimal
67+
];
68+
})
69+
global
70+
nixConf
71+
tmp
72+
];
73+
perms =
74+
args.perms
75+
or []
76+
++ [
77+
{
78+
path = tmp;
79+
regex = ".*";
80+
mode = "1777";
81+
}
82+
];
83+
};
784
}

nix/cloud/oci-images.nix

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,31 @@
22
inputs,
33
cell,
44
}: let
5-
inherit (inputs.cicero.packages) cicero-entrypoint cicero webhook-trigger cicero-evaluator-nix;
6-
inherit (inputs.nixpkgs) lib cacert symlinkJoin closureInfo runCommandNoCC;
7-
inherit (inputs.n2c.packages.nix2container) buildImage buildLayer;
5+
inherit (inputs.cicero.packages) webhook-trigger;
6+
inherit (inputs.nixpkgs) symlinkJoin;
7+
inherit (inputs.n2c.packages.nix2container) buildImage;
88
in {
99
# jq < result '.layers | map({size: .size, paths: .paths | map(.path)}) | sort_by(.size) | .[11].paths[]' -r | xargs du -sch
10-
cicero = let
11-
# This must include all dependencies used in the image to allow nix builds
12-
# in the container (Nix won't know about paths otherwise and fail on trying
13-
# to write read-only data).
14-
closure = closureInfo {
15-
rootPaths = {
16-
inherit (cell.entrypoints) cicero;
17-
inherit cacert;
18-
};
19-
};
20-
21-
global = runCommandNoCC "global" {} ''
22-
mkdir -p $out $out/etc
23-
cp ${closure}/registration $out
24-
25-
echo 'root:x:0:' > $out/etc/group
26-
echo 'nixbld:x:30000:nixbld1' > $out/etc/group
27-
28-
echo 'root:!:0:0::/local:/bin/bash' > $out/etc/passwd
29-
echo 'nixbld1:!:30001:30000:Nix build user 1:/var/empty:/bin/nologin' >> $out/etc/passwd
30-
'';
31-
32-
nixConf = runCommandNoCC "nix.conf" {} ''
33-
mkdir -p $out/etc/nix
34-
cat > $out/etc/nix/nix.conf <<'EOF'
35-
# If /dev/kvm does not actually exist in the container
36-
# we would rather build without KVM than fail.
37-
extra-system-features = kvm
38-
39-
experimental-features = nix-command flakes
40-
EOF
41-
'';
42-
43-
tmp = runCommandNoCC "tmp" {} ''
44-
mkdir -p $out/tmp
45-
'';
46-
in
47-
buildImage {
10+
cicero = buildImage (
11+
cell.library.addN2cNixArgs {
12+
inherit (cell.entrypoints) cicero;
13+
} {
4814
name = "registry.ci.iog.io/cicero";
4915
tag = "main"; # keep in sync with branch name of flake input
5016
config.Cmd = ["${cell.entrypoints.cicero}/bin/entrypoint"];
51-
config.Env = lib.mapAttrsToList (n: v: "${n}=${v}") {
52-
SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt";
53-
};
5417
maxLayers = 60;
5518
contents = [
5619
(symlinkJoin {
5720
name = "root";
5821
paths = with inputs.nixpkgs; [
5922
# for transformers
6023
jq
61-
# `bash` would also be ok for transformers
62-
# but we may as well get the interactive version
63-
# for manual debugging
64-
bashInteractive
65-
66-
coreutils
67-
strace
24+
bash
6825
];
6926
})
70-
global
71-
nixConf
72-
tmp
73-
];
74-
perms = [
75-
{
76-
path = tmp;
77-
regex = ".*";
78-
mode = "0777";
79-
}
8027
];
81-
};
28+
}
29+
);
8230

8331
webhook-trigger = buildImage {
8432
name = "registry.ci.iog.io/webhook-trigger";

0 commit comments

Comments
 (0)