Skip to content

Instantly share code, notes, and snippets.

@FRidh
Created May 11, 2018 15:39
Show Gist options
  • Save FRidh/7b5ccbddf01575faf43d83dd2a04418b to your computer and use it in GitHub Desktop.
Save FRidh/7b5ccbddf01575faf43d83dd2a04418b to your computer and use it in GitHub Desktop.

Build artifacts with Nix and Docker

Nix is a package manager, and Docker is a tool for running containers. Nix allows sandboxed builds, but only when running the daemon as root. That's not always possible (corporate) but Docker is more commonly available. So, we use the sandboxing features of Docker for producing builds with Nix.

Files

  • Makefile is the entry point. Run make nix to build the default.nix expression
  • default.nix describes what we would like to build
  • Dockerfile describes the container that is used for building
# Nix expression to build during container *runtime*.
# This defines only what build-artifacts we want.
let
# Revision of Nixpkgs
revision = "45cd6090d9a6d2661f7e8b9b7e98ba0aba11316c";
url = "https://github.com/NixOS/nixpkgs/archive/${revision}.tar.gz";
pkgs = import (fetchTarball url) {};
# List of a attributes we want to build
builds = pkgs.linkFarm "builds" [
{name = "python27"; path = pkgs.python27;}
# {name = "python35"; path = pkgs.python35;}
# {name = "python36"; path = pkgs.python36;}
];
in builds
# Get nix 2.0 image from https://hub.docker.com/r/nixos/nix/
FROM nixos/nix:2.0
# Include the Nix expression we want to build
ADD default.nix default.nix
# We want the possibility for a custom store location
# Note that by using "/" there is effectively no mount namespace
# opening the doors for impurities.
ARG MOUNT_NIX_STORE
ENV NIX_REMOTE ""
ENV NIX_STORE_DIR $MOUNT_NIX_STORE/nix/store
ENV NIX_STATE_DIR $MOUNT_NIX_STORE/nix/var
# Run Nix command when running the container.
# The final output (when all goes well) are the store paths produced.
# Note that because we're in a container we can only pass in
# files that can be seen inside the container. In this case,
# we've already added the file to the image with ADD.
# CMD = ["nix-build"]
ENTRYPOINT ["nix-build"]
Copyright (c) 2018 Frederik Rietdijk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Run `nix-build` inside a Docker container.
MOUNT_NIX_STORE = /home/freddy/nix_store_docker
# Docker image
image:
docker build --build-arg MOUNT_NIX_STORE=$(MOUNT_NIX_STORE) -t nix-docker-python .
# Nix store folder
store:
mkdir -p $(MOUNT_NIX_STORE)
# Nix expression
nix: image store
docker run --mount type=bind,source=$(MOUNT_NIX_STORE),target=$(MOUNT_NIX_STORE) nix-docker-python:latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment