
How can I create minimal containers from nixpkgs - without extra dependencies?
One of my reasons for taking an interest in NixOS, was the idea of assembling containers more easily from prebuilt packages, rather than running "apt-get install". The advantages are numerous.
But are the nixpkgs usable for containerization? What extra steps should you take? For example:
- do you create a wrapper package, that only copies over necessary files - or removes unneeded ones?
- do you compile statically linked for reduced binary size and simpler filesystem layout?
- ...
I'm currently facing an issue with the 'squid' package, in that it pulls in a bunch of other packages.
image = pkgs.dockerTools.buildLayeredImage {
name = "squid";
tag = "latest";
contents = with pkgs; [
coreutils
squid
];
extraCommands = ''
mkdir -p var/lib/squid
'';
fakeRootCommands = ''
chown -R 1000:1000 var/*
chmod 700 var/*
'';
config = {
User = "1000";
Cmd = [
"/sbin/sh"
];
};
};
This pulls in systemd (/nix/store/11yzh5vk22ybx1abvs42184ydf2hpdj3-systemd-258.7/), which is obviously not needed.
Reviewing the package source code, I therefore tried override,
(squid.override { systemd = null; libcap = null; pam = null; })
and this builds an image without this package.
But it also requires a full rebuild. A major advantage was in being able to use prebuilt packages. I suppose I can use a binary cache, so the hit wont happen so frequently, but it's significant if this has to be done for every package, especially since systemd service definitions are common.
What are my alternatives?
Is there a way to use prebuilt packages, but remove the dependencies after?