CCache: Difference between revisions

From NixOS Wiki
imported>Milahu
m Milahu moved page Ccache to CCache
imported>Milahu
use sandbox-paths
Line 3: Line 3:
with ccache, recompile time can be reduced from many hours to a few minutes
with ccache, recompile time can be reduced from many hours to a few minutes


== derivation ccache ==
== add ccache to sandbox ==
 
to enable ccache only for one derivation


add to <code>/etc/nixos/configuration.nix</code>
add to <code>/etc/nixos/configuration.nix</code>
Line 11: Line 9:
<pre>
<pre>
           nix.extraOptions = ''
           nix.extraOptions = ''
             sandbox = false
             extra-sandbox-paths = /nix/var/cache/ccache
           '';
           '';
</pre>
</pre>
todo: use <code>nix.sandboxPaths</code>? [https://github.com/NixOS/nixpkgs/issues/153343]


run <code>sudo nixos-rebuild switch</code>
run <code>sudo nixos-rebuild switch</code>


now <code>/etc/nix/nix.conf</code> should have <code>sandbox = false</code>
now <code>/etc/nix/nix.conf</code> should have <code>sandbox-paths = /nix/var/cache/ccache</code>
 
create the cache folder
 
<pre>
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'
</pre>
 
== derivation ccache ==
 
to enable ccache only for one derivation


patch the <code>default.nix</code> file
patch the <code>default.nix</code> file
Line 31: Line 47:


   preConfigure = ''
   preConfigure = ''
     export CCACHE_DIR=/var/cache/ccache
     export CCACHE_DIR=/nix/var/cache/ccache
     export CCACHE_UMASK=007
     export CCACHE_UMASK=007
   ''
   ''
Line 50: Line 66:
<pre>
<pre>
# watch ccache size
# watch ccache size
sudo watch du -sh /var/cache/ccache
sudo watch du -sh /nix/var/cache/ccache


# watch ccache stats
# watch ccache stats
sudo watch ccache --dir /var/cache/ccache --show-stats --verbose
sudo watch ccache --dir /nix/var/cache/ccache --show-stats --verbose
</pre>
</pre>
== see also ==
* https://leanprover.github.io/lean4/doc/make/nix.html
* [https://nixos.org/manual/nix/stable/command-ref/conf-file.html sandbox-paths in nix.conf]

Revision as of 19:26, 6 January 2022

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

create 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

see also