Skip to content

Instantly share code, notes, and snippets.

@mattpolzin
Created August 4, 2024 23:36
Show Gist options
  • Select an option

  • Save mattpolzin/7c4f71df30ef695391aa4697be90e19d to your computer and use it in GitHub Desktop.

Select an option

Save mattpolzin/7c4f71df30ef695391aa4697be90e19d to your computer and use it in GitHub Desktop.
Working nginx Docker Image from Nix derivation
{ nixpkgs ? import <nixpkgs> {} }:
let inherit (nixpkgs) busybox lib writeText runCommand nginx;
adduser = "${busybox}/bin/adduser";
files = runCommand "mk-files" {} ''
mkdir -p $out/share
echo '<html>hi</html>' > $out/share/index.html
'';
nginxConf = writeText "nginx-conf" ''
events {}
http {
server {
listen 8080;
location / {
autoindex on;
root ${files}/share;
}
}
}
'';
in
nixpkgs.dockerTools.buildLayeredImage {
name = "nginx2";
enableFakechroot = true;
fakeRootCommands = ''
mkdir -p /etc
touch /etc/passwd
${adduser} nginx -H
mkdir -p /var/log/nginx
chown nginx /var/log/nginx
ln -sf /dev/stdout /var/log/nginx/access.log
ln -sf /dev/stderr /var/log/nginx/error.log
mkdir -p /tmp
chown nginx /tmp
'';
config = {
User = "nginx";
Cmd = [
"${lib.getExe nginx}"
"-g"
"daemon off;"
"-c"
"${nginxConf}"
];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment