forked from jmgilman/dev-container
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
117 lines (93 loc) · 3.75 KB
/
Dockerfile
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
ARG USER=vscode
ARG UID=1000
ARG GID=${UID}
ARG NIX_VERSION=nix-2.13.2
FROM debian:stable-slim as base
ARG USER
ARG UID
ARG GID
ARG NIX_VERSION
# ARG NIX_INSTALLER=https://nixos.org/nix/install
ARG NIX_INSTALLER=https://mirrors.tuna.tsinghua.edu.cn/nix/${NIX_VERSION}/install
# Set shell and check for pipe fails
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# Install deps required by Nix installer
RUN sed -i "s/deb.debian.org/mirrors.ustc.edu.cn/g" /etc/apt/sources.list && \
apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends \
ca-certificates \
curl \
sudo \
xz-utils
# Create user
RUN groupadd -g ${GID} ${USER} && \
useradd -u ${UID} -g ${GID} -G sudo -m ${USER} -s /bin/bash
# Configure sudo and Nix
RUN sed -i 's/%sudo.*ALL/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/' /etc/sudoers && \
echo "sandbox = false" > /etc/nix.conf && \
echo "experimental-features = nix-command flakes" >> /etc/nix.conf && \
echo "substituters = https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store https://cache.nixos.org/" >> /etc/nix.conf
# need use the mirror cache to download nix-command
# Install Nix and enable flakes
USER ${USER}
ENV USER=${USER}
ENV NIX_PATH=/home/${USER}/.nix-defexpr/channels:/nix/var/nix/profiles/per-user/root/channels
ENV NIX_CONF_DIR /etc
RUN curl -L ${NIX_INSTALLER} | NIX_INSTALLER_NO_MODIFY_PROFILE=1 sh
# Copy configs
RUN mkdir -p /home/${USER}/.config/devcontainer/extra
COPY --chown=${USER}:${USER} config/flake.nix /home/${USER}/.config/devcontainer/flake.nix
COPY --chown=${USER}:${USER} config/flake.lock /home/${USER}/.config/devcontainer/flake.lock
COPY --chown=${USER}:${USER} config/config.nix /home/${USER}/.config/devcontainer/config.nix
# Fix architecture and user
RUN sed -i "s/ARCH/$(uname -m)-$(uname -s | tr '[:upper:]' '[:lower:]')/" /home/${USER}/.config/devcontainer/flake.nix
RUN sed -i "s/USER/${USER}/" /home/${USER}/.config/devcontainer/flake.nix
# Build home-manager
WORKDIR /home/${USER}/.config/devcontainer
RUN . /home/${USER}/.nix-profile/etc/profile.d/nix.sh && \
nix build --no-link .#homeConfigurations.${USER}.activationPackage
FROM debian:stable-slim
ARG USER
ARG UID
ARG GID
ENV NIX_CONF_DIR /etc
ENV DIRENV_CONFIG /etc
## Install vscode deps
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends \
ca-certificates \
git \
gnupg \
locales \
ssh \
sudo && \
rm -rf /var/lib/apt/lists/*
# Create user
RUN groupadd -g ${GID} ${USER} && \
useradd -u ${UID} -g ${GID} -G sudo -m ${USER} -s /bin/bash
COPY --from=base --chown=${USER}:${USER} /home/${USER} /home/${USER}
# Configure en_US.UTF-8 locale
RUN echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
# Configure sudo
RUN sed -i 's/%sudo.*ALL/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/' /etc/sudoers
# Setup Nix environment
RUN echo "source /home/${USER}/.nix-profile/etc/profile.d/nix.sh" >> /etc/bash.bashrc && \
echo "source /home/${USER}/.nix-profile/etc/profile.d/nix.sh" >> /etc/zshrc
# Copy nix and configs
COPY --from=base /nix /nix
COPY --from=base /etc/nix.conf /etc/nix.conf
COPY config/direnv.toml /etc
USER ${USER}
# Setup vscode
RUN mkdir -p /home/${USER}/.vscode-server/extensions && \
mkdir -p /home/${USER}/.vscode-server-insiders/extensions
# Switch to home-manager environment
WORKDIR /home/${USER}/.config/devcontainer
ENV USER=${USER}
ARG NIX_VERSION
RUN . /home/${USER}/.nix-profile/etc/profile.d/nix.sh && \
nix-env --set-flag priority 10 ${NIX_VERSION} && \
"$(nix path-info .#homeConfigurations.${USER}.activationPackage)"/activate
#nix version should be the same of one from the shell
# Copy entrypoint
COPY docker-entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT [ "/docker-entrypoint.sh" ]