From 4259f7575e5a715bf4001e22894ce95cdc06385a Mon Sep 17 00:00:00 2001 From: Ding Xiang Fei Date: Tue, 30 Oct 2018 15:44:00 +0800 Subject: [PATCH 1/2] use closure-info for building system tarball --- nixos/lib/make-system-tarball.nix | 20 ++++++++++++-------- nixos/lib/make-system-tarball.sh | 9 +++------ nixos/modules/profiles/docker-container.nix | 16 ++++++++++------ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/nixos/lib/make-system-tarball.nix b/nixos/lib/make-system-tarball.nix index 846013b02d1..dee91a6ce3f 100644 --- a/nixos/lib/make-system-tarball.nix +++ b/nixos/lib/make-system-tarball.nix @@ -1,4 +1,4 @@ -{ stdenv, perl, pixz, pathsFromGraph +{ stdenv, closureInfo, pixz , # The file name of the resulting tarball fileName ? "nixos-system-${stdenv.hostPlatform.system}" @@ -29,24 +29,28 @@ , extraInputs ? [ pixz ] }: +let + symlinks = map (x: x.symlink) storeContents; + objects = map (x: x.object) storeContents; +in + stdenv.mkDerivation { name = "tarball"; builder = ./make-system-tarball.sh; - buildInputs = [ perl ] ++ extraInputs; + buildInputs = extraInputs; - inherit fileName pathsFromGraph extraArgs extraCommands compressCommand; + inherit fileName extraArgs extraCommands compressCommand; # !!! should use XML. sources = map (x: x.source) contents; targets = map (x: x.target) contents; # !!! should use XML. - objects = map (x: x.object) storeContents; - symlinks = map (x: x.symlink) storeContents; + inherit symlinks objects; - # For obtaining the closure of `storeContents'. - exportReferencesGraph = - map (x: [("closure-" + baseNameOf x.object) x.object]) storeContents; + closureInfo = closureInfo { + rootPaths = objects; + }; extension = compressionExtension; } diff --git a/nixos/lib/make-system-tarball.sh b/nixos/lib/make-system-tarball.sh index 1a52a284a25..1a0017a1799 100644 --- a/nixos/lib/make-system-tarball.sh +++ b/nixos/lib/make-system-tarball.sh @@ -3,7 +3,6 @@ source $stdenv/setup sources_=($sources) targets_=($targets) -echo $objects objects=($objects) symlinks=($symlinks) @@ -14,8 +13,6 @@ stripSlash() { if test "${res:0:1}" = /; then res=${res:1}; fi } -touch pathlist - # Add the individual files. for ((i = 0; i < ${#targets_[@]}; i++)); do stripSlash "${targets_[$i]}" @@ -25,9 +22,9 @@ done # Add the closures of the top-level store objects. +chmod +w . mkdir -p nix/store -storePaths=$(perl $pathsFromGraph closure-*) -for i in $storePaths; do +for i in $(< $closureInfo/store-paths); do cp -a "$i" "${i:1}" done @@ -35,7 +32,7 @@ done # TODO tar ruxo # Also include a manifest of the closures in a format suitable for # nix-store --load-db. -printRegistration=1 perl $pathsFromGraph closure-* > nix-path-registration +cp $closureInfo/registration nix-path-registration # Add symlinks to the top-level store objects. for ((n = 0; n < ${#objects[*]}; n++)); do diff --git a/nixos/modules/profiles/docker-container.nix b/nixos/modules/profiles/docker-container.nix index 7031d7d1d59..5d6b11498b5 100644 --- a/nixos/modules/profiles/docker-container.nix +++ b/nixos/modules/profiles/docker-container.nix @@ -15,15 +15,19 @@ in { # Create the tarball system.build.tarball = pkgs.callPackage ../../lib/make-system-tarball.nix { - contents = []; + contents = [ + { + source = "${config.system.build.toplevel}/."; + target = "./"; + } + ]; extraArgs = "--owner=0"; # Add init script to image - storeContents = [ - { object = config.system.build.toplevel + "/init"; - symlink = "/init"; - } - ] ++ (pkgs2storeContents [ pkgs.stdenv ]); + storeContents = pkgs2storeContents [ + config.system.build.toplevel + pkgs.stdenv + ]; # Some container managers like lxc need these extraCommands = "mkdir -p proc sys dev"; From 50daffc4b828894e076c190fbd88bac19148b4f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 23 Nov 2018 15:40:10 +0000 Subject: [PATCH 2/2] nixos/docker-image: add example usage --- nixos/modules/virtualisation/docker-image.nix | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/nixos/modules/virtualisation/docker-image.nix b/nixos/modules/virtualisation/docker-image.nix index 2f304094d55..baac3a35a78 100644 --- a/nixos/modules/virtualisation/docker-image.nix +++ b/nixos/modules/virtualisation/docker-image.nix @@ -17,3 +17,41 @@ # Socket activated ssh presents problem in Docker. services.openssh.startWhenNeeded = false; } + +# Example usage: +# +## default.nix +# let +# nixos = import { +# configuration = ./configuration.nix; +# system = "x86_64-linux"; +# }; +# in +# nixos.config.system.build.tarball +# +## configuration.nix +# { pkgs, config, lib, ... }: +# { +# imports = [ +# +# +# ]; +# +# documentation.doc.enable = false; +# +# environment.systemPackages = with pkgs; [ +# bashInteractive +# cacert +# nix +# ]; +# } +# +## Run +# Build the tarball: +# $ nix-build default.nix +# Load into docker: +# $ docker import result/tarball/nixos-system-*.tar.xz nixos-docker +# Boots into systemd +# $ docker run --privileged -it nixos-docker /init +# Log into the container +# $ docker exec -it /run/current-system/sw/bin/bash