2018-09-17 16:00:23 +00:00
|
|
|
"""BuildPack for nixpkgs environments"""
|
|
|
|
import os
|
|
|
|
|
2022-10-31 22:32:14 +00:00
|
|
|
from ..base import BaseImage, BuildPack
|
2018-09-17 16:00:23 +00:00
|
|
|
|
|
|
|
|
2018-10-19 15:57:40 +00:00
|
|
|
class NixBuildPack(BaseImage):
|
2018-09-17 16:00:23 +00:00
|
|
|
"""A nix Package Manager BuildPack"""
|
|
|
|
|
|
|
|
def get_path(self):
|
2020-09-10 06:11:29 +00:00
|
|
|
"""Return paths to be added to PATH environemnt variable"""
|
2019-05-31 09:10:17 +00:00
|
|
|
return super().get_path() + ["/home/${NB_USER}/.nix-profile/bin"]
|
2018-09-17 16:00:23 +00:00
|
|
|
|
|
|
|
def get_env(self):
|
|
|
|
"""Ordered list of environment variables to be set for this image"""
|
2023-01-01 15:52:19 +00:00
|
|
|
|
2018-09-17 16:00:23 +00:00
|
|
|
return super().get_env() + [
|
2019-05-31 09:10:17 +00:00
|
|
|
("NIX_PATH", "nixpkgs=/home/${NB_USER}/.nix-defexpr/channels/nixpkgs"),
|
|
|
|
("NIX_SSL_CERT_FILE", "/etc/ssl/certs/ca-certificates.crt"),
|
|
|
|
("GIT_SSL_CAINFO", "/etc/ssl/certs/ca-certificates.crt"),
|
2018-09-17 16:00:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_build_scripts(self):
|
|
|
|
"""
|
|
|
|
Return series of build-steps common to all nix repositories.
|
|
|
|
Notice how only root privileges are needed for creating nix
|
2020-11-28 09:31:52 +00:00
|
|
|
directory and a nix.conf file.
|
2018-09-17 16:00:23 +00:00
|
|
|
|
|
|
|
- create nix directory for user nix installation
|
2020-11-28 09:31:52 +00:00
|
|
|
- disable sandboxing because its unsupported inside a Docker container
|
2018-09-17 16:00:23 +00:00
|
|
|
- install nix package manager for user
|
2020-11-28 09:31:52 +00:00
|
|
|
|
2018-09-17 16:00:23 +00:00
|
|
|
"""
|
2023-01-01 15:52:19 +00:00
|
|
|
if self.platform == "linux-aarch64":
|
|
|
|
nix_arch = "aarch64"
|
|
|
|
else:
|
|
|
|
nix_arch = "x86_64"
|
2018-09-17 16:00:23 +00:00
|
|
|
return super().get_build_scripts() + [
|
2019-05-31 09:10:17 +00:00
|
|
|
(
|
|
|
|
"root",
|
|
|
|
"""
|
2018-09-17 16:00:23 +00:00
|
|
|
mkdir -m 0755 /nix && \
|
2020-11-28 09:31:52 +00:00
|
|
|
chown -R ${NB_USER}:${NB_USER} /nix /usr/local/bin/nix-shell-wrapper /home/${NB_USER} && \
|
|
|
|
mkdir -p /etc/nix && \
|
|
|
|
touch /etc/nix/nix.conf && \
|
|
|
|
echo "sandbox = false" >> /etc/nix/nix.conf
|
2019-05-31 09:10:17 +00:00
|
|
|
""",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"${NB_USER}",
|
2023-01-01 15:52:19 +00:00
|
|
|
f"""
|
|
|
|
NIX_ARCH={nix_arch} bash /home/${{NB_USER}}/.local/bin/install-nix.bash && \
|
|
|
|
rm /home/${{NB_USER}}/.local/bin/install-nix.bash
|
2019-05-31 09:10:17 +00:00
|
|
|
""",
|
|
|
|
),
|
2018-09-17 16:00:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_build_script_files(self):
|
2020-09-10 06:11:29 +00:00
|
|
|
"""Dict of files to be copied to the container image for use in building"""
|
2018-09-17 16:00:23 +00:00
|
|
|
return {
|
|
|
|
"nix/install-nix.bash": "/home/${NB_USER}/.local/bin/install-nix.bash",
|
2019-05-31 09:10:17 +00:00
|
|
|
"nix/nix-shell-wrapper": "/usr/local/bin/nix-shell-wrapper",
|
2018-09-17 16:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_assemble_scripts(self):
|
2020-09-10 06:11:29 +00:00
|
|
|
"""Return series of build-steps specific to this source repository."""
|
2018-09-17 16:00:23 +00:00
|
|
|
return super().get_assemble_scripts() + [
|
2019-05-31 09:10:17 +00:00
|
|
|
(
|
|
|
|
"${NB_USER}",
|
2022-10-23 17:18:37 +00:00
|
|
|
f"""
|
2018-09-17 16:00:23 +00:00
|
|
|
nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs && \
|
|
|
|
nix-channel --update && \
|
2022-10-23 17:18:37 +00:00
|
|
|
nix-shell {self.binder_path("default.nix")}
|
|
|
|
""",
|
2019-05-31 09:10:17 +00:00
|
|
|
)
|
2018-09-17 16:00:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_start_script(self):
|
|
|
|
"""The path to a script to be executed as ENTRYPOINT"""
|
2018-10-19 15:57:40 +00:00
|
|
|
# the shell wrapper script duplicates the behaviour of other buildpacks
|
2018-11-09 07:10:34 +00:00
|
|
|
# when it comes to the `start` script as well as handling a binder/
|
|
|
|
# sub-directory when it exists
|
2018-09-17 16:00:23 +00:00
|
|
|
return "/usr/local/bin/nix-shell-wrapper"
|
|
|
|
|
|
|
|
def detect(self):
|
|
|
|
"""Check if current repo should be built with the nix BuildPack"""
|
2019-05-31 09:10:17 +00:00
|
|
|
return os.path.exists(self.binder_path("default.nix"))
|