NixOS offers the use of ccache for building packages by configuring programs.ccache.packageNames
, which will in turn configure nixpkgs.overlays
. This will replace the pkgs
attribute of the system configuration with a new one that now also includes this ccache overlay.
In my Flake-based configuration, I prefer to import nixpkgs as rarely as possible and configuring ccache this way would mean that I would add another evaluation of nixpkgs to my build.
Right now, I have something like this in my flake.nix
:
...
let
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
config = {allowUnfree = true;};
overlays = [overlay1 overlay2 overlay3 ...];
};
in {
nixosConfigurations = {
myHost = nixpkgs.lib.nixosSystem {
inherit system;
inherit pkgs;
modules = [
# ...
];
};
};
}
...
If I wanted to add a ccache overlay here, I would probably create an overlay similar to this:
(self: super: {
ccacheWrapper = super.ccacheWrapper.override {
extraConfig = ''
export CCACHE_COMPRESS=1
export CCACHE_DIR="/nix/var/cache/ccache"
export CCACHE_UMASK=007
if [ ! -d "$CCACHE_DIR" ]; then
echo "====="
echo "Directory '$CCACHE_DIR' does not exist"
echo "Please create it with:"
echo " sudo mkdir -m0770 '$CCACHE_DIR'"
echo " sudo chown root:nixbld '$CCACHE_DIR'"
echo "====="
exit 1
fi
if [ ! -w "$CCACHE_DIR" ]; then
echo "====="
echo "Directory '$CCACHE_DIR' is not accessible for user $(whoami)"
echo "Please verify its access permissions"
echo "====="
exit 1
fi
'';
};
ffmpeg = super.ffmpeg.override { stdenv = super.ccacheStdenv; };
})
Using this overlay would make assumptions about the target system, like the presence of and access to /nix/var/cache/ccache
.
Additionally, we would need to add this path to nix.settings.extra-sandbox-paths
, which would mean that every host needs to have an additional option here.
Usually I would solve issues like these using common NixOS modules, but we are dealing with importing nixpkgs here before we even declare a NixOS configuration.
I also wonder if I would be creating a chicken and egg problem here, as a (new?) system, that doesn’t yet include its cache path in the Nix sandbox wouldn’t be able to build overidden packages until nix.settings.extra-sandbox-paths
has been adjusted.
How are other people dealing with this, or are there maybe alternative approaches?