|
1 | | -FROM lopsided/archlinux-arm64v8:devel |
2 | | - |
3 | | -# Install additional dependencies |
4 | | -RUN pacman -Syu --noconfirm && \ |
5 | | - pacman -S --noconfirm \ |
6 | | - curl \ |
7 | | - git \ |
8 | | - sudo \ |
9 | | - zsh && \ |
10 | | - pacman -Scc --noconfirm |
11 | | - |
12 | | -# Create user |
| 1 | +# Stage 1: Builder — install Nix, run home-manager switch |
| 2 | +FROM ubuntu:24.04 AS builder |
| 3 | +SHELL ["/bin/bash", "-o", "pipefail", "-c"] |
13 | 4 | ARG USERNAME |
14 | | -RUN useradd -m -s /bin/zsh ${USERNAME} && \ |
15 | | - echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers |
| 5 | +ARG UID=1000 |
| 6 | +ARG GID=1000 |
16 | 7 |
|
17 | | -USER ${USERNAME} |
18 | | -WORKDIR /home/${USERNAME} |
| 8 | +RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 9 | + curl=8.5.0-2ubuntu10.7 \ |
| 10 | + ca-certificates=20240203 \ |
| 11 | + xz-utils=5.6.1+really5.4.5-1ubuntu0.2 \ |
| 12 | + git=1:2.43.0-1ubuntu7.3 \ |
| 13 | + && rm -rf /var/lib/apt/lists/* |
| 14 | + |
| 15 | +RUN userdel -r ubuntu 2>/dev/null; groupdel ubuntu 2>/dev/null; \ |
| 16 | + groupadd -g ${GID} ${USERNAME} \ |
| 17 | + && useradd -l -m -u ${UID} -g ${GID} -s /bin/bash ${USERNAME} |
| 18 | + |
| 19 | +RUN curl --proto '=https' --tlsv1.2 -sSf -L \ |
| 20 | + https://install.determinate.systems/nix | sh -s -- install linux \ |
| 21 | + --extra-conf "trusted-users = root ${USERNAME}" \ |
| 22 | + --init none --no-confirm |
19 | 23 |
|
20 | | -# Install Nix |
21 | | -RUN curl -L https://nixos.org/nix/install | sh -s -- --no-daemon |
| 24 | +ENV PATH="/nix/var/nix/profiles/default/bin:${PATH}" |
| 25 | +RUN mkdir -p /etc/nix \ |
| 26 | + && echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf |
22 | 27 |
|
23 | | -# Source Nix in shell |
24 | | -RUN echo ". $HOME/.nix-profile/etc/profile.d/nix.sh" >> "$HOME/.zshrc" |
| 28 | +# Prepare home directory structure for home-manager activation |
| 29 | +RUN mkdir -p /home/${USERNAME}/.config/nix \ |
| 30 | + /home/${USERNAME}/.local/state/nix/profiles \ |
| 31 | + /home/${USERNAME}/.local/state/home-manager \ |
| 32 | + /nix/var/nix/profiles/per-user/${USERNAME} \ |
| 33 | + && chown -R ${UID}:${GID} /home/${USERNAME} |
25 | 34 |
|
26 | | -# Copy Nix configuration |
27 | | -COPY --chown=${USERNAME}:${USERNAME} . /home/${USERNAME}/.config/nix/ |
| 35 | +# Layer cache: flake lock changes rarely, so fetch deps first |
| 36 | +COPY flake.nix flake.lock /home/${USERNAME}/.config/nix/ |
| 37 | +RUN chown -R ${UID}:${GID} /home/${USERNAME}/.config/nix |
| 38 | + |
| 39 | +ENV USER=${USERNAME} HOME=/home/${USERNAME} |
| 40 | + |
| 41 | +# Pre-fetch flake deps (cached until flake.lock changes) |
| 42 | +RUN nix-daemon & sleep 1 \ |
| 43 | + && su -s /bin/sh ${USERNAME} -c " \ |
| 44 | + export PATH=/nix/var/nix/profiles/default/bin:\$PATH USER=${USERNAME} HOME=/home/${USERNAME} \ |
| 45 | + && cd /home/${USERNAME}/.config/nix \ |
| 46 | + && nix flake archive" |
| 47 | + |
| 48 | +# Now copy full config and build |
| 49 | +COPY . /home/${USERNAME}/.config/nix/ |
| 50 | +RUN chown -R ${UID}:${GID} /home/${USERNAME}/.config/nix |
28 | 51 |
|
29 | | -# Apply home-manager configuration |
30 | 52 | WORKDIR /home/${USERNAME}/.config/nix |
31 | | -ENV PATH="/home/${USERNAME}/.nix-profile/bin:${PATH}" |
| 53 | +RUN set -e; \ |
| 54 | + export PROFILE; \ |
| 55 | + PROFILE=$(if [ "$(uname -m)" = "aarch64" ]; then echo minimal-arm; else echo minimal-x86; fi); \ |
| 56 | + nix-daemon & sleep 1; \ |
| 57 | + su -s /bin/sh ${USERNAME} -c " \ |
| 58 | + export PATH=/nix/var/nix/profiles/default/bin:\$PATH USER=${USERNAME} HOME=/home/${USERNAME} \ |
| 59 | + && cd /home/${USERNAME}/.config/nix \ |
| 60 | + && nix run home-manager -- switch --flake .#\$PROFILE -b backup" |
| 61 | + |
| 62 | +# Prune build-only deps from the store |
| 63 | +RUN nix-daemon & sleep 1 \ |
| 64 | + && nix-collect-garbage -d \ |
| 65 | + && rm -rf /nix/var/nix/temproots/* /nix/var/log/nix/* |
| 66 | + |
| 67 | +# ----------------------------------------------------------- |
| 68 | +# Stage 2: Runtime — slim glibc base, everything useful comes from Nix |
| 69 | +FROM debian:bookworm-slim |
32 | 70 | ARG USERNAME |
33 | | -ENV USER=${USERNAME} |
34 | | -RUN nix run home-manager -- switch --flake .#minimal-arm -b backup |
| 71 | +ARG UID=1000 |
| 72 | +ARG GID=1000 |
35 | 73 |
|
36 | | -WORKDIR /home/${USERNAME} |
| 74 | +RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 75 | + ca-certificates=20230311+deb12u1 \ |
| 76 | + locales=2.36-9+deb12u13 \ |
| 77 | + && sed -i 's/^# *\(en_US.UTF-8\)/\1/' /etc/locale.gen \ |
| 78 | + && locale-gen \ |
| 79 | + && rm -rf /var/lib/apt/lists/* \ |
| 80 | + && groupadd -g ${GID} ${USERNAME} \ |
| 81 | + && useradd -l -m -u ${UID} -g ${GID} -s /bin/bash ${USERNAME} |
| 82 | + |
| 83 | +ENV LANG=en_US.UTF-8 \ |
| 84 | + LC_ALL=en_US.UTF-8 |
| 85 | + |
| 86 | +# Copy pruned Nix store and user profile (ownership preserved from builder) |
| 87 | +COPY --from=builder /nix /nix |
| 88 | +COPY --from=builder /home/${USERNAME} /home/${USERNAME} |
37 | 89 |
|
38 | | -CMD ["/bin/zsh"] |
| 90 | +ENV USER=${USERNAME} \ |
| 91 | + HOME=/home/${USERNAME} \ |
| 92 | + PATH="/home/${USERNAME}/.nix-profile/bin:/nix/var/nix/profiles/default/bin:${PATH}" \ |
| 93 | + NIX_PROFILES="/nix/var/nix/profiles/default /home/${USERNAME}/.nix-profile" \ |
| 94 | + NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt \ |
| 95 | + SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt \ |
| 96 | + CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt \ |
| 97 | + GIT_SSL_CAINFO=/etc/ssl/certs/ca-certificates.crt |
| 98 | + |
| 99 | +USER ${USERNAME} |
| 100 | +WORKDIR /home/${USERNAME} |
| 101 | +CMD ["zsh", "-l"] |
0 commit comments