Skip to content

examples: avoid hard-coding x86_64-linux #8

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

Merged
merged 2 commits into from
Dec 28, 2021
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
2 changes: 1 addition & 1 deletion examples/golang-httpserver-flake/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```bash
export DOCKER_BUILDKIT=1
docker build -t golang-httpserver -f default.nix .
docker build -t golang-httpserver -f flake.nix .
```

```
Expand Down
16 changes: 16 additions & 0 deletions examples/golang-httpserver-flake/flake.lock

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

42 changes: 22 additions & 20 deletions examples/golang-httpserver-flake/flake.nix
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# syntax = ghcr.io/akihirosuda/buildkit-nix:v0.0.2@sha256:ad13161464806242fd69dbf520bd70a15211b557d37f61178a4bf8e1fd39f1f2

{
outputs = { self, nixpkgs }:
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
# See https://ryantm.github.io/nixpkgs/languages-frameworks/go/
app = pkgs.buildGoModule {
name = "golang-httpserver";
src = ./.;
vendorSha256 = "FdDIvZrvGFHk7aqjLtJsiqsIHM6lob9iNPLd7ITau7o=";
runVend = true;
};
in {
defaultPackage.x86_64-linux = pkgs.dockerTools.buildImage {
name = "golang-httpserver";
tag = "nix";
contents = [ pkgs.bash pkgs.coreutils app ];
config = {
Cmd = [ "golang-httpserver" ];
ExposedPorts = { "80/tcp" = { }; };
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
# See https://ryantm.github.io/nixpkgs/languages-frameworks/go/
app = pkgs.buildGoModule {
name = "golang-httpserver";
src = ./.;
vendorSha256 = "FdDIvZrvGFHk7aqjLtJsiqsIHM6lob9iNPLd7ITau7o=";
runVend = true;
};
};
};
in rec {
defaultPackage = pkgs.dockerTools.buildImage {
name = "golang-httpserver";
tag = "nix";
contents = [ pkgs.bash pkgs.coreutils app ];
config = {
Cmd = [ "golang-httpserver" ];
ExposedPorts = { "80/tcp" = { }; };
};
};
});
}
16 changes: 16 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.

90 changes: 46 additions & 44 deletions examples/nginx-flake/flake.nix
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
# syntax = ghcr.io/akihirosuda/buildkit-nix:v0.0.2@sha256:ad13161464806242fd69dbf520bd70a15211b557d37f61178a4bf8e1fd39f1f2

{
outputs = { self, nixpkgs }:
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
entrypoint = pkgs.writeScript "docker-entrypoint.sh" ''
#!${pkgs.stdenv.shell}
set -eux -o pipefail
# Create "nogroup" group
if ! grep -q ^nogroup /etc/group; then
# dereference symlink (/etc/group -> /nix/....) to support `docker run --read-only`
cp -aL /etc/group /etc/group.real
echo nogroup:x:65534: >>/etc/group.real
rm -f /etc/group
mv /etc/group.real /etc/group
fi
# Initialize /var
mkdir -p /var/log/nginx /var/cache/nginx/client_body
exec nginx -g "daemon off; error_log /dev/stderr debug;"
'';
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
gnugrep
nginx
(writeTextDir "${pkgs.nginx}/html/index.html" ''
<html><body>hello nix</body></html>
'')
];
# runAsRoot cannot be used because it depends on KVM.
# extraCommands can be used but often fails unless running everything as the root.
# https://discourse.nixos.org/t/dockertools-buildimage-and-user-writable-tmp/5397
config = {
Cmd = [ entrypoint ];
ExposedPorts = { "80/tcp" = { }; };
Volumes = {
"/etc" = { };
"/var" = { };
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
entrypoint = pkgs.writeScript "docker-entrypoint.sh" ''
#!${pkgs.stdenv.shell}
set -eux -o pipefail
# Create "nogroup" group
if ! grep -q ^nogroup /etc/group; then
# dereference symlink (/etc/group -> /nix/....) to support `docker run --read-only`
cp -aL /etc/group /etc/group.real
echo nogroup:x:65534: >>/etc/group.real
rm -f /etc/group
mv /etc/group.real /etc/group
fi
# Initialize /var
mkdir -p /var/log/nginx /var/cache/nginx/client_body
exec nginx -g "daemon off; error_log /dev/stderr debug;"
'';
in with pkgs; rec {
defaultPackage = 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
gnugrep
nginx
(writeTextDir "${pkgs.nginx}/html/index.html" ''
<html><body>hello nix</body></html>
'')
];
# runAsRoot cannot be used because it depends on KVM.
# extraCommands can be used but often fails unless running everything as the root.
# https://discourse.nixos.org/t/dockertools-buildimage-and-user-writable-tmp/5397
config = {
Cmd = [ entrypoint ];
ExposedPorts = { "80/tcp" = { }; };
Volumes = {
"/etc" = { };
"/var" = { };
};
};
};
};
};
});
}