CCache

From NixOS Wiki
Revision as of 19:49, 5 October 2022 by Atry (talk | contribs)
Jump to: navigation, search

CCache is useful for Packaging large packages with Incremental builds.

With CCache, recompile time can be reduced from many hours to a few minutes.

Add CCache to sandbox

Add to /etc/nixos/configuration.nix

nix.extraOptions = ''
  extra-sandbox-paths = /nix/var/cache/ccache
'';

TODO: use nix.sandboxPaths? [1]

Run sudo nixos-rebuild switch

Now /etc/nix/nix.conf should have sandbox-paths = /nix/var/cache/ccache

Ceate the cache folder

sudo mkdir -m0770 -p /nix/var/cache/ccache

# Linux
sudo chown --reference=/nix/store /nix/var/cache/ccache

# macOS workaround for chown --reference
nix-shell -p coreutils --run 'sudo chown --reference=/nix/store /nix/var/cache/ccache'

Derivation CCache

To enable CCache only for one derivation, patch the default.nix file

{ stdenv
, ccacheStdenv
# ...
}:

#stdenv.mkDerivation {
ccacheStdenv.mkDerivation {

  preConfigure = ''
    export CCACHE_DIR=/nix/var/cache/ccache
    export CCACHE_UMASK=007
  '';

# ...

Add ccacheStdenv to dependencies.

Run nix-build.

System CCache

todo

Monitor CCache status

# watch ccache size
sudo watch du -sh /nix/var/cache/ccache

# watch ccache stats
sudo watch ccache --dir /nix/var/cache/ccache --show-stats --verbose

Uncacheable

TODO: why are some compilations "uncacheable" according to ccache --show-stats

See also