Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockerTools: Add example of using NixOS' etc #175474

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nixos/tests/docker-tools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -419,5 +419,10 @@ import ./make-test-python.nix ({ pkgs, ... }: {
"docker rmi layered-image-with-path",
)

with subtest("etc"):
docker.succeed("${examples.etc} | docker load")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not need to be

Suggested change
docker.succeed("${examples.etc} | docker load")
docker.succeed("cat ${examples.etc} | docker load")

or

Suggested change
docker.succeed("${examples.etc} | docker load")
docker.succeed("docker load -i ${examples.etc}")

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually a script, so the current thing works.
It's more efficient to use streamLayeredImage than buildLayeredImage because we don't have to store and later GC the image.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh

docker.succeed("docker run --rm etc | grep localhost")
docker.succeed("docker image rm etc:latest")

'';
})
41 changes: 41 additions & 0 deletions pkgs/build-support/docker/examples.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@

{ pkgs, buildImage, buildLayeredImage, fakeNss, pullImage, shadowSetup, buildImageWithNixDb, pkgsCross }:

let
nixosLib = import ../../../nixos/lib {
# Experimental features need testing too, but there's no point in warning
# about it, so we enable the feature flag.
featureFlags.minimalModules = {};
};
evalMinimalConfig = module: nixosLib.evalModules { modules = [ module ]; };

in

rec {
# 1. basic example
bash = buildImage {
Expand Down Expand Up @@ -582,6 +592,37 @@ rec {
includeStorePaths = false;
};

etc =
let
inherit (pkgs) lib;
nixosCore = (evalMinimalConfig ({ config, ... }: {
imports = [
pkgs.pkgsModule
../../../nixos/modules/system/etc/etc.nix
];
environment.etc."some-config-file" = {
text = ''
127.0.0.1 localhost
::1 localhost
'';
# For executables:
# mode = "0755";
};
}));
in pkgs.dockerTools.streamLayeredImage {
name = "etc";
tag = "latest";
enableFakechroot = true;
fakeRootCommands = ''
mkdir -p /etc
${nixosCore.config.system.build.etcActivationCommands}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a function in dockerTools that just takes a module and runs the whole activation script, but I don't think the modules are in a state to support that yet. Until then, this is the best we can do.

'';
config.Cmd = pkgs.writeScript "etc-cmd" ''
#!${pkgs.busybox}/bin/sh
${pkgs.busybox}/bin/cat /etc/some-config-file
'';
};

# Example export of the bash image
exportBash = pkgs.dockerTools.exportImage { fromImage = bash; };

Expand Down