Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base nix on ubuntu, no nix-shell for prod image, prod-image 1.47GB #100

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

VERSION 0.8

nix:
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y curl \
&& curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux \
--extra-conf "sandbox = false" \
--init none \
--no-confirm \
&& apt remove --auto-remove -y curl
ENV PATH="${PATH}:/nix/var/nix/profiles/default/bin"

nix-dev-shell:
ARG --required DEVSHELL
FROM nixos/nix:2.20.4
# enable flakes
RUN echo "extra-experimental-features = nix-command flakes" >> /etc/nix/nix.conf
FROM +nix
# replace /bin/sh with a script that sources `/root/sh_env` for every RUN command.
# we use this to execute all `RUN`-commands in our nix dev shell.
# we need to explicitly delete `/bin/sh` first, because it's a symlink to `/bin/busybox`,
Expand All @@ -21,9 +29,20 @@ nix-dev-shell:
WORKDIR /app
COPY flake.nix flake.lock .
# build our dev-shell, creating a gcroot, so it won't be garbage collected by nix.
RUN nix build --out-link /root/flake-devShell-gcroot ".#devShells.$ARCH-linux.$DEVSHELL"
# set up our `/root/sh_env` file to source our flake env, will be used by ALL `RUN`-commands!
RUN nix print-dev-env ".#$DEVSHELL" >> /root/sh_env
# Then set up our `/root/sh_env` file to source our flake env, will be used by ALL `RUN`-commands!
RUN nix build --out-link /root/flake-devShell-gcroot ".#devShells.$ARCH-linux.$DEVSHELL" \
&& nix print-dev-env ".#$DEVSHELL" >> /root/sh_env \
&& nix-collect-garbage

nix-packages:
ARG --required PACKAGES
FROM +nix
WORKDIR /app
COPY flake.nix flake.lock .
RUN nix profile install --impure ".#$PACKAGES" \
&& nix-collect-garbage \
&& rm -rf /root/.cache
# RUN nix store optimise # hard linking can save ~40M


globalbrain-node-package:
Expand Down Expand Up @@ -72,7 +91,7 @@ app-deploy-litefs:
SAVE ARTIFACT /usr/local/bin/litefs

docker-image:
FROM +nix-dev-shell --DEVSHELL='production'
FROM +nix-packages --PACKAGES='production'

WORKDIR /app

Expand Down Expand Up @@ -105,16 +124,16 @@ docker-image:
ENV INTERNAL_PORT="8080"
ENV PORT="8081"

RUN nix-collect-garbage
RUN du -sh /* \
&& find /app -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -sh | sort -hr | head -20 \
&& find /nix/store -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -sh | sort -hr | head -20 \
&& find /app/node_modules -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -sh | sort -hr | head -20
RUN find / \( -type f -o -type d \) -mindepth 1 -maxdepth 1 -print0 | xargs -0 du -sh | sort -hr | head -20 \
&& find /nix/store \( -type f -o -type d \) -mindepth 1 -maxdepth 1 -print0 | xargs -0 du -sh | sort -hr | head -20 \
&& find /app \( -type f -o -type d \) -mindepth 1 -maxdepth 1 -print0 | xargs -0 du -sh | sort -hr | head -20 \
&& find /app/node_modules \( -type f -o -type d \) -mindepth 1 -maxdepth 1 -print0 | xargs -0 du -sh | sort -hr | head -20 \
&& find /root \( -type f -o -type d \) -mindepth 1 -maxdepth 1 -print0 | xargs -0 du -sh | sort -hr | head -20
JohannesNakayama marked this conversation as resolved.
Show resolved Hide resolved

# starting the application is defined in litefs.yml
# test locally without litefs:
# docker run -e SESSION_SECRET -e INTERNAL_COMMAND_TOKEN -e HONEYPOT_SECRET sha256:xyzxyz /bin/sh startup.sh
CMD ["/bin/sh", "-c", "/usr/local/bin/litefs mount"]
CMD ["/usr/local/bin/litefs", "mount"]
SAVE IMAGE jabble:latest

docker-image-e2e-test:
Expand All @@ -129,8 +148,7 @@ docker-image-e2e-test:
WORKDIR /app
COPY flake.nix flake.lock .
RUN nix develop ".#e2e" --command echo warmed up
COPY --dir e2e playwright.config.ts ./
COPY docker-compose.yml ./
COPY --dir e2e playwright.config.ts docker-compose.yml ./
WITH DOCKER --load jabble:latest=+docker-image
RUN docker image ls \
&& (docker-compose up &) \
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ services:
- SESSION_SECRET=super-duper-s3cret
- HONEYPOT_SECRET=super-duper-s3cret
- INTERNAL_COMMAND_TOKEN=some-made-up-token
command: /bin/sh startup.sh
command: ./startup.sh
init: true # make ctrl+c work
restart: no
8 changes: 5 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@
export PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=true
'';
};
production = pkgs.mkShellNoCC {
buildInputs = with pkgs; [
};
packages = {
production = pkgs.buildEnv {
name = "production";
paths = with pkgs; [
nodejs_20
sqlite-interactive
fuse3 # for litefs
busybox # for swap tools
];
};
};
Expand Down
2 changes: 1 addition & 1 deletion fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source = "data"
destination = "/data"

[deploy]
release_command = "/bin/sh -c 'node ./other/sentry-create-release'"
release_command = "node ./other/sentry-create-release"

[[services]]
internal_port = 8_080
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ download-prod-db:
rm -f "$APP_DATABASE_PATH"
rm -f "$APP_DATABASE_PATH"-shm
rm -f "$APP_DATABASE_PATH"-wal
flyctl ssh console -C "/bin/sh -c \"sqlite3 /litefs/data/sqlite.db '.backup /data/backup.db'\""
flyctl ssh console -C "sqlite3 /litefs/data/sqlite.db '.backup /data/backup.db'"
flyctl ssh sftp get /data/backup.db "$APP_DATABASE_PATH" || true

# build the docker container
Expand Down
4 changes: 2 additions & 2 deletions other/litefs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ lease:
key: 'litefs/${FLY_APP_NAME}'

exec:
- cmd: /bin/sh -c "node ./other/setup-swap.js"
- cmd: /bin/sh startup.sh
- cmd: node ./other/setup-swap.js
- cmd: ./startup.sh
2 changes: 1 addition & 1 deletion other/recent-sessions.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
#!/usr/bin/env bash

sqlite3 -column $APP_DATABASE_PATH $'select username, datetime(session.createdAt/1000, \'unixepoch\') as login_time_utc from session join user on userId = user.id order by session.createdAt desc limit 10;'