CCache: Difference between revisions

From NixOS Wiki
imported>Milahu
No edit summary
imported>Fufexan
Capitalize, add category
Line 1: Line 1:
ccache is useful for [[Packaging]] large packages with [[Incremental builds]]
CCache is useful for [[Packaging]] large packages with [[Incremental builds]].


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.


== add ccache to sandbox ==
== Add CCache to sandbox ==


add to <code>/etc/nixos/configuration.nix</code>
Add to <code>/etc/nixos/configuration.nix</code>


<pre>
<syntaxhighlight lang="nix">
          nix.extraOptions = ''
nix.extraOptions = ''
            extra-sandbox-paths = /nix/var/cache/ccache
  extra-sandbox-paths = /nix/var/cache/ccache
          '';
'';
</pre>
</syntaxhighlight>


todo: use <code>nix.sandboxPaths</code>? [https://github.com/NixOS/nixpkgs/issues/153343]
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-paths = /nix/var/cache/ccache</code>
Now <code>/etc/nix/nix.conf</code> should have <code>sandbox-paths = /nix/var/cache/ccache</code>


create the cache folder
Ceate the cache folder


<pre>
<pre>
Line 31: Line 31:
</pre>
</pre>


== derivation ccache ==
== Derivation CCache ==
 
to enable ccache only for one derivation


patch the <code>default.nix</code> file
To enable CCache only for one derivation, patch the <code>default.nix</code> file


<pre>
<syntaxhighlight lang="nix">
{ stdenv
{ stdenv
, ccacheStdenv
, ccacheStdenv
Line 52: Line 50:


# ...
# ...
</pre>
</syntaxhighlight>


add <code>ccacheStdenv</code> to dependencies
Add <code>ccacheStdenv</code> to dependencies.


run <code>nix-build</code>
Run <code>nix-build</code>.


== system ccache ==
== System CCache ==


todo
todo


== monitor ccache status ==
== Monitor CCache status ==


<pre>
<pre>
Line 72: Line 70:
</pre>
</pre>


== uncacheable ==
== Uncacheable ==


todo: why are some compilations "uncacheable" according to <code>ccache --show-stats</code>
TODO: why are some compilations "uncacheable" according to <code>ccache --show-stats</code>


== see also ==
== See also ==


* https://leanprover.github.io/lean4/doc/make/nix.html
* 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]
* [https://nixos.org/manual/nix/stable/command-ref/conf-file.html sandbox-paths in nix.conf]
[[Category: Development]]

Revision as of 20:18, 9 August 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

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