Skip to content

Commit 97fd8a1

Browse files
committed
Add dev/prod conditions in the nix files
1 parent b8e2af4 commit 97fd8a1

3 files changed

Lines changed: 60 additions & 42 deletions

File tree

api/default.nix

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{pkgs, nosocket, ...}:
1+
{pkgs, nosocket, appEnv, ...}:
22
with pkgs; rec {
33
package = mkYarnPackage {
44
name = "piston";
@@ -25,45 +25,54 @@ with pkgs; rec {
2525
};
2626
};
2727
};
28+
29+
basePackages = with pkgs; [
30+
package
31+
nosocket
32+
bash
33+
nixFlakes
34+
coreutils-full
35+
cacert.out
36+
git
37+
gnutar
38+
gzip
39+
gnugrep
40+
rename
41+
util-linux
42+
];
43+
devPackages = with pkgs; [
44+
nodejs-16_x
45+
yarn
46+
python3
47+
gcc
48+
gnumake
49+
gnused
50+
];
51+
52+
baseCommands = ''
53+
mkdir -p piston/{jobs,runtimes} etc/nix {,var/}tmp run/lock
54+
echo -e "experimental-features = nix-command flakes" >> etc/nix/nix.conf
55+
echo "nixbld:x:30000:nixbld1,nixbld10,nixbld11,nixbld12,nixbld13,nixbld14,nixbld15,nixbld16,nixbld17,nixbld18,nixbld19,nixbld2,nixbld20,nixbld21,nixbld22,nixbld23,nixbld24,nixbld25,nixbld26,nixbld27,nixbld28,nixbld29,nixbld3,nixbld30,nixbld31,nixbld32,nixbld4,nixbld5,nixbld6,nixbld7,nixbld8,nixbld9" >> etc/group
56+
for i in $(seq 1 32)
57+
do
58+
echo "nixbld$i:x:$(( $i + 30000 )):30000:Nix build user $i:/var/empty:/run/current-system/sw/bin/nologin" >> etc/passwd
59+
done
60+
61+
chmod 1777 {,var/}tmp/
62+
'';
63+
devCommands = ''
64+
mkdir -p usr/bin
65+
ln -s /bin/env usr/bin/env
66+
chmod -R 1777 usr
67+
'';
68+
2869
container = pkgs.dockerTools.buildLayeredImageWithNixDb {
2970
name = "piston";
3071
tag = "base-latest";
3172

32-
contents = with pkgs; [
33-
package
34-
nosocket
35-
bash
36-
nixFlakes
37-
coreutils-full
38-
cacert.out
39-
git
40-
gnutar
41-
gzip
42-
gnugrep
43-
rename
44-
util-linux
45-
nodejs-16_x
46-
yarn
47-
python3
48-
gcc
49-
gnumake
50-
gnused
51-
];
52-
53-
extraCommands = ''
54-
mkdir -p piston/{jobs,runtimes} etc/nix {,var/}tmp run/lock
55-
echo -e "experimental-features = nix-command flakes" >> etc/nix/nix.conf
56-
echo "nixbld:x:30000:nixbld1,nixbld10,nixbld11,nixbld12,nixbld13,nixbld14,nixbld15,nixbld16,nixbld17,nixbld18,nixbld19,nixbld2,nixbld20,nixbld21,nixbld22,nixbld23,nixbld24,nixbld25,nixbld26,nixbld27,nixbld28,nixbld29,nixbld3,nixbld30,nixbld31,nixbld32,nixbld4,nixbld5,nixbld6,nixbld7,nixbld8,nixbld9" >> etc/group
57-
for i in $(seq 1 32)
58-
do
59-
echo "nixbld$i:x:$(( $i + 30000 )):30000:Nix build user $i:/var/empty:/run/current-system/sw/bin/nologin" >> etc/passwd
60-
done
73+
contents = if appEnv == "dev" then basePackages ++ devPackages else basePackages;
6174

62-
mkdir -p usr/bin
63-
ln -s /bin/env usr/bin/env
64-
chmod -R 1777 usr
65-
chmod 1777 {,var/}tmp/
66-
'';
75+
extraCommands = if appEnv == "dev" then baseCommands + devCommands else baseCommands;
6776

6877
config = {
6978
Cmd = ["${package}/bin/pistond"];

flake.nix

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
let
77
system = "x86_64-linux";
88
pkgs = nixpkgs.legacyPackages.${system};
9-
baseContainer = (import ./api {
10-
inherit pkgs;
11-
nosocket = self.legacyPackages."${system}".nosocket;
9+
_nosocket = self.legacyPackages."${system}".nosocket;
10+
_devContainer = (import ./api {
11+
inherit pkgs _nosocket;
12+
nosocket = _nosocket;
13+
appEnv = "dev";
14+
}).container;
15+
_prodContainer = (import ./api {
16+
inherit pkgs _nosocket;
17+
nosocket = _nosocket;
18+
appEnv = "prod";
1219
}).container;
1320
args = {
1421
inherit pkgs;
@@ -74,6 +81,7 @@
7481
piston = (import ./api { inherit pkgs nosocket; }).package;
7582
};
7683

77-
container = baseContainer;
84+
devContainer = _devContainer;
85+
prodContainer = _prodContainer;
7886
};
7987
}

piston

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ IMAGE_NAME_DEV="piston"
1212
SUBCOMMAND="$1"
1313
shift
1414

15-
build_base(){
16-
CONTAINER_PATH="$(nix build ".#container" --no-link --json | jq '.[0].outputs.out' -r)"
15+
build_base() {
16+
container_flake_key=$([[ $1 == "dev" ]] && echo "devContainer" || echo "prodContainer")
17+
CONTAINER_PATH="$(nix build ".#$container_flake_key" --no-link --json | jq '.[0].outputs.out' -r)"
1718
echo "The image archive was created at: $CONTAINER_PATH"
1819
docker load -i $CONTAINER_PATH || exit 1
1920
docker tag "$IMAGE_NAME_DEV:$IMAGE_TAG" "$IMAGE_NAME:$IMAGE_TAG" || exit 1
@@ -64,7 +65,7 @@ case "$SUBCOMMAND" in
6465
echo "Removing the Nix volume if it exists"
6566
docker volume rm -f $DEV_VOLUME_NAME
6667
echo "Building the base docker image"
67-
build_base
68+
build_base dev
6869
echo "Installing the required node modules"
6970
docker run \
7071
--rm \

0 commit comments

Comments
 (0)