Skip to content

Commit

Permalink
Merge pull request #4 from AkihiroSuda/dev
Browse files Browse the repository at this point in the history
Support flake
  • Loading branch information
AkihiroSuda authored Dec 28, 2021
2 parents 95a6304 + 7acf3b3 commit 85458a3
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ jobs:
sudo apt-get install -y moreutils
- name: "Run the integration test"
run: ./hack/test.sh
- name: "Show the local images"
run: docker images --no-trunc
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# BuildKit-Nix: Nix as Dockerfiles (`docker build -f default.nix .`)

BuildKit-Nix allows using Nix derivations (`default.nix`) as Dockerfiles.
BuildKit-Nix allows using Nix derivations (`default.nix`, `flake.nix`) as Dockerfiles.

## Examples

- [`./examples/nginx/default.nix`](./examples/nginx/default.nix)
- [`./examples/nginx/default.nix`](./examples/nginx/default.nix): Nginx (Legacy)
- [`./examples/nginx-flake/flake.nix`](./examples/nginx-flake/flake.nix): Nginx (Flake)

## Usage
### With Docker
Expand Down
19 changes: 16 additions & 3 deletions cmd/buildkit-nix/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,25 @@ func helperAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
// nix-build creates a docker V1 tgz to /context/result .
nixBuildCmd := exec.CommandContext(ctx, "nix-build", "--option", "build-users-group", "", fullFilename)
flakeMode := filepath.Base(filename) == "flake.nix"
// nix creates a docker V1 tgz to /context/result .
var nixBuildCmd *exec.Cmd
if flakeMode {
nixBuildCmd = exec.CommandContext(ctx,
"nix",
"--extra-experimental-features", "nix-command",
"--extra-experimental-features", "flakes",
"build",
"--option", "build-users-group", "",
"/dockerfile",
)
} else {
nixBuildCmd = exec.CommandContext(ctx, "nix-build", "--option", "build-users-group", "", fullFilename)
}
nixBuildCmd.Dir = "/context"
nixBuildCmd.Stderr = cmd.OutOrStderr()
nixBuildCmd.Stdout = nixBuildCmd.Stderr
logrus.Infof("Running %v", nixBuildCmd.Args)
logrus.Infof("Running %v (flake mode: %v)", nixBuildCmd.Args, flakeMode)
if err := nixBuildCmd.Run(); err != nil {
return err
}
Expand Down
23 changes: 23 additions & 0 deletions examples/nginx-flake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Example: nginx (Flake)

## Usage

```bash
export DOCKER_BUILDKIT=1
docker build -t nginx-nix -f flake.nix .
```

```
docker run -d -p 8080:80 --name nginx-nix nginx-nix
```

## Updating dependencies

```bash
nix flake update --override-input nixpkgs github:NixOS/nixpkgs/master
```

You might have to create `~/.config/nix/nix.conf` with the following content:
```
experimental-features = nix-command flakes
```
25 changes: 25 additions & 0 deletions examples/nginx-flake/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions examples/nginx-flake/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# syntax = ghcr.io/akihirosuda/buildkit-nix:latest

{
outputs = { self, nixpkgs }:
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in
with pkgs; {
defaultPackage.x86_64-linux = dockerTools.buildImage {
name = "nginx";
tag = "nix";
contents = [
# fakeNss creates /etc/passwd and /etc/group (https://github.com/NixOS/nixpkgs/blob/e548124f/pkgs/build-support/docker/default.nix#L741-L763)
dockerTools.fakeNss
bash
coreutils
nginx
(writeTextDir "${pkgs.nginx}/html/index.html" ''
<html><body>hello nix</body></html>
'')
];
# Use extraCommands, not runAsRoot, to avoid dependency on KVM
extraCommands = "
grep -q ^nogroup etc/group || echo nogroup:x:65534: >>etc/group
mkdir -p var/log/nginx var/cache/nginx/client_body
";
config = {
Cmd = [ "nginx" "-g" "daemon off; error_log /dev/stderr debug;" ];
ExposedPorts = {
"80/tcp" = { };
};
};
};
};
}
19 changes: 19 additions & 0 deletions examples/nginx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Example: nginx

## Usage

```bash
export DOCKER_BUILDKIT=1
docker build -t nginx-nix -f default.nix .
```

```
docker run -d -p 8080:80 --name nginx-nix nginx-nix
```

## Updating dependencies

```bash
nix-env -i niv
niv update
```
1 change: 1 addition & 0 deletions examples/nginx/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pkgs.dockerTools.buildImage{
<html><body>hello nix</body></html>
'')
];
# Use extraCommands, not runAsRoot, to avoid dependency on KVM
extraCommands = "
grep -q ^nogroup etc/group || echo nogroup:x:65534: >>etc/group
mkdir -p var/log/nginx var/cache/nginx/client_body
Expand Down
6 changes: 3 additions & 3 deletions examples/nginx/nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "c64e26628b8b2cc958fe5cfad66c49f44473f8e3",
"sha256": "0i6ll77yszmkk0by94ayn47jjamdprvs3v0zhmbhg5pmnhcr5xcw",
"rev": "f03e55c9fc9d013adb2b4c6d61a2c78956c6ee6a",
"sha256": "03krwlw73lw4nmgj47ds1qmvcf1xpsl46g78b01kg7xklpiqwcxy",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/c64e26628b8b2cc958fe5cfad66c49f44473f8e3.tar.gz",
"url": "https://github.com/NixOS/nixpkgs/archive/f03e55c9fc9d013adb2b4c6d61a2c78956c6ee6a.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}
24 changes: 16 additions & 8 deletions hack/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ image="127.0.0.1:5000/buildkit-nix:test-${version}-${timestamp}"
"$DOCKER" build -t "$image" -f bootstrap.Dockerfile .
"$DOCKER" push "$image"

(
cd "${td}/examples/nginx"
sed -i '1 s/^ *# *syntax *=.*$//' default.nix
for f in "${td}"/examples/*; do
name="$(basename "${f}")"
echo "===== ${name} ====="
(
echo "# syntax = ${image}"
cat default.nix
) | sponge default.nix
"$DOCKER" build -t nginx-nix -f default.nix .
)
cd "$f"
df="default.nix"
if [[ -e "flake.nix" ]]; then
df="flake.nix"
fi
sed -i '1 s/^ *# *syntax *=.*$//' "${df}"
(
echo "# syntax = ${image}"
cat "${df}"
) | sponge "${df}"
"$DOCKER" build -t "${name}" -f "${df}" .
)
done

0 comments on commit 85458a3

Please sign in to comment.