-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9854 from the-sun-will-rise-tomorrow/docker-user
docker: Allow building for non-root user
- Loading branch information
Showing
6 changed files
with
163 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env bash | ||
# docker.nix test script. Runs inside a built docker.nix container. | ||
|
||
set -eEuo pipefail | ||
|
||
export NIX_CONFIG='substituters = http://cache:5000?trusted=1' | ||
|
||
cd /tmp | ||
|
||
# Test getting a fetched derivation | ||
test "$("$(nix-build -E '(import <nixpkgs> {}).hello')"/bin/hello)" = "Hello, world!" | ||
|
||
# Test building a simple derivation | ||
# shellcheck disable=SC2016 | ||
nix-build -E ' | ||
let | ||
pkgs = import <nixpkgs> {}; | ||
in | ||
builtins.derivation { | ||
name = "test"; | ||
system = builtins.currentSystem; | ||
builder = "${pkgs.bash}/bin/bash"; | ||
args = ["-c" "echo OK > $out"]; | ||
}' | ||
test "$(cat result)" = OK | ||
|
||
# Ensure #!/bin/sh shebang works | ||
echo '#!/bin/sh' > ./shebang-test | ||
echo 'echo OK' >> ./shebang-test | ||
chmod +x ./shebang-test | ||
test "$(./shebang-test)" = OK | ||
|
||
# Ensure #!/usr/bin/env shebang works | ||
echo '#!/usr/bin/env bash' > ./shebang-test | ||
echo 'echo OK' >> ./shebang-test | ||
chmod +x ./shebang-test | ||
test "$(./shebang-test)" = OK | ||
|
||
# Test nix-shell | ||
{ | ||
echo '#!/usr/bin/env nix-shell' | ||
echo '#! nix-shell -i bash' | ||
echo '#! nix-shell -p hello' | ||
echo 'hello' | ||
} > ./nix-shell-test | ||
chmod +x ./nix-shell-test | ||
test "$(./nix-shell-test)" = "Hello, world!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Test the container built by ../../docker.nix. | ||
|
||
{ lib, config, nixpkgs, hostPkgs, ... }: | ||
|
||
let | ||
pkgs = config.nodes.machine.nixpkgs.pkgs; | ||
|
||
nixImage = import ../../docker.nix { | ||
inherit (config.nodes.machine.nixpkgs) pkgs; | ||
}; | ||
nixUserImage = import ../../docker.nix { | ||
inherit (config.nodes.machine.nixpkgs) pkgs; | ||
name = "nix-user"; | ||
uid = 1000; | ||
gid = 1000; | ||
uname = "user"; | ||
gname = "user"; | ||
}; | ||
|
||
containerTestScript = ./nix-docker-test.sh; | ||
|
||
in { | ||
name = "nix-docker"; | ||
|
||
nodes = | ||
{ machine = | ||
{ config, lib, pkgs, ... }: | ||
{ virtualisation.diskSize = 4096; | ||
}; | ||
cache = | ||
{ config, lib, pkgs, ... }: | ||
{ virtualisation.additionalPaths = [ pkgs.stdenv pkgs.hello ]; | ||
services.harmonia.enable = true; | ||
networking.firewall.allowedTCPPorts = [ 5000 ]; | ||
}; | ||
}; | ||
|
||
testScript = { nodes }: '' | ||
cache.wait_for_unit("harmonia.service") | ||
machine.succeed("mkdir -p /etc/containers") | ||
machine.succeed("""echo '{"default":[{"type":"insecureAcceptAnything"}]}' > /etc/containers/policy.json""") | ||
machine.succeed("${pkgs.podman}/bin/podman load -i ${nixImage}") | ||
machine.succeed("${pkgs.podman}/bin/podman run --rm nix nix --version") | ||
machine.succeed("${pkgs.podman}/bin/podman run --rm -i nix < ${containerTestScript}") | ||
machine.succeed("${pkgs.podman}/bin/podman load -i ${nixUserImage}") | ||
machine.succeed("${pkgs.podman}/bin/podman run --rm nix-user nix --version") | ||
machine.succeed("${pkgs.podman}/bin/podman run --rm -i nix-user < ${containerTestScript}") | ||
machine.succeed("[[ $(${pkgs.podman}/bin/podman run --rm nix-user stat -c %u /nix/store) = 1000 ]]") | ||
''; | ||
} |