git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #105685 from flokli/dockertools-fakenss

dockerTools.fakeNss: init

commit7a34bcc2a32493b204192cabb898f025cd4ba008 authorFlorian Klink <flokli@flokli.de> dateWed 2 Dec 2020, 23:34 (6 years ago) parent96f34a8e054694
2 files changed+37−11
pkgs/build-support/docker/default.nix+30−1 pkgs/build-support/docker/examples.nix+7−10
pkgs/build-support/docker/default.nix+30−1
@@ -1,4 +1,5 @@
11{
2+ bashInteractive,
23 buildPackages,
34 cacert,
45 callPackage,
@@ -29,6 +30,7 @@
2930 writeReferencesToFile,
3031 writeScript,
3132 writeText,
33+ writeTextDir,
3234 writePython3,
3335 system, # Note: This is the cross system we're compiling for
3436}:
@@ -70,7 +72,7 @@ in
7072rec {
7173
7274 examples = callPackage ./examples.nix {
73− inherit buildImage pullImage shadowSetup buildImageWithNixDb;
75+ inherit buildImage buildLayeredImage fakeNss pullImage shadowSetup buildImageWithNixDb;
7476 };
7577
7678 pullImage = let
@@ -684,6 +686,33 @@ rec {
684686 in
685687 result;
686688
689+ # Provide a /etc/passwd and /etc/group that contain root and nobody.
690+ # Useful when packaging binaries that insist on using nss to look up
691+ # username/groups (like nginx).
692+ # /bin/sh is fine to not exist, and provided by another shim.
693+ fakeNss = symlinkJoin {
694+ name = "fake-nss";
695+ paths = [
696+ (writeTextDir "etc/passwd" ''
697+ root:x:0:0:root user:/var/empty:/bin/sh
698+ nobody:x:65534:65534:nobody:/var/empty:/bin/sh
699+ '')
700+ (writeTextDir "etc/group" ''
701+ root:x:0:
702+ nobody:x:65534:
703+ '')
704+ (runCommand "var-empty" {} ''
705+ mkdir -p $out/var/empty
706+ '')
707+ ];
708+ };
709+
710+ # This provides /bin/sh, pointing to bashInteractive.
711+ binSh = runCommand "bin-sh" {} ''
712+ mkdir -p $out/bin
713+ ln -s ${bashInteractive}/bin/bash $out/bin/sh
714+ '';
715+
687716 # Build an image and populate its nix database with the provided
688717 # contents. The main purpose is to be able to use nix commands in
689718 # the container.
pkgs/build-support/docker/examples.nix+7−10
@@ -7,7 +7,7 @@
77# $ nix-build '<nixpkgs>' -A dockerTools.examples.redis
88# $ docker load < result
99
10−{ pkgs, buildImage, pullImage, shadowSetup, buildImageWithNixDb, pkgsCross }:
10+{ pkgs, buildImage, buildLayeredImage, fakeNss, pullImage, shadowSetup, buildImageWithNixDb, pkgsCross }:
1111
1212rec {
1313 # 1. basic example
@@ -44,7 +44,7 @@ rec {
4444 nginx = let
4545 nginxPort = "80";
4646 nginxConf = pkgs.writeText "nginx.conf" ''
47− user nginx nginx;
47+ user nobody nobody;
4848 daemon off;
4949 error_log /dev/stdout info;
5050 pid /dev/null;
@@ -64,10 +64,13 @@ rec {
6464 <html><body><h1>Hello from NGINX</h1></body></html>
6565 '';
6666 in
67− buildImage {
67+ buildLayeredImage {
6868 name = "nginx-container";
6969 tag = "latest";
70− contents = pkgs.nginx;
70+ contents = [
71+ fakeNss
72+ pkgs.nginx
73+ ];
7174
7275 extraCommands = ''
7376 # nginx still tries to read this directory even if error_log
@@ -75,12 +78,6 @@ rec {
7578 mkdir -p var/log/nginx
7679 mkdir -p var/cache/nginx
7780 '';
78− runAsRoot = ''
79− #!${pkgs.stdenv.shell}
80− ${shadowSetup}
81− groupadd --system nginx
82− useradd --system --gid nginx nginx
83− '';
8481
8582 config = {
8683 Cmd = [ "nginx" "-c" nginxConf ];