2020-04-24 23:36:52 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.programs.ccache;
|
|
|
|
in {
|
|
|
|
options.programs.ccache = {
|
|
|
|
# host configuration
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = lib.mkEnableOption "CCache, a compiler cache for fast recompilation of C/C++ code";
|
2024-02-29 20:09:43 +00:00
|
|
|
cacheDir = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "CCache directory";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "/var/cache/ccache";
|
|
|
|
};
|
|
|
|
# target configuration
|
2024-02-29 20:09:43 +00:00
|
|
|
packageNames = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Nix top-level packages to be compiled using CCache";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = [];
|
2023-03-24 00:07:29 +00:00
|
|
|
example = [ "wxGTK32" "ffmpeg" "libav_all" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2024-02-29 20:09:43 +00:00
|
|
|
owner = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "root";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Owner of CCache directory";
|
2024-02-29 20:09:43 +00:00
|
|
|
};
|
|
|
|
group = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "nixbld";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Group owner of CCache directory";
|
2024-02-29 20:09:43 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
config = lib.mkMerge [
|
2020-04-24 23:36:52 +00:00
|
|
|
# host configuration
|
2024-02-29 20:09:43 +00:00
|
|
|
(lib.mkIf cfg.enable {
|
|
|
|
systemd.tmpfiles.rules = [ "d ${cfg.cacheDir} 0770 ${cfg.owner} ${cfg.group} -" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# "nix-ccache --show-stats" and "nix-ccache --clear"
|
|
|
|
security.wrappers.nix-ccache = {
|
2024-02-29 20:09:43 +00:00
|
|
|
inherit (cfg) owner group;
|
2021-09-22 15:38:15 +00:00
|
|
|
setuid = false;
|
2020-04-24 23:36:52 +00:00
|
|
|
setgid = true;
|
|
|
|
source = pkgs.writeScript "nix-ccache.pl" ''
|
|
|
|
#!${pkgs.perl}/bin/perl
|
|
|
|
|
|
|
|
%ENV=( CCACHE_DIR => '${cfg.cacheDir}' );
|
|
|
|
sub untaint {
|
|
|
|
my $v = shift;
|
|
|
|
return '-C' if $v eq '-C' || $v eq '--clear';
|
|
|
|
return '-V' if $v eq '-V' || $v eq '--version';
|
|
|
|
return '-s' if $v eq '-s' || $v eq '--show-stats';
|
|
|
|
return '-z' if $v eq '-z' || $v eq '--zero-stats';
|
|
|
|
exec('${pkgs.ccache}/bin/ccache', '-h');
|
|
|
|
}
|
|
|
|
exec('${pkgs.ccache}/bin/ccache', map { untaint $_ } @ARGV);
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
# target configuration
|
2024-02-29 20:09:43 +00:00
|
|
|
(lib.mkIf (cfg.packageNames != []) {
|
2020-04-24 23:36:52 +00:00
|
|
|
nixpkgs.overlays = [
|
2024-02-29 20:09:43 +00:00
|
|
|
(self: super: lib.genAttrs cfg.packageNames (pn: super.${pn}.override { stdenv = builtins.trace "with ccache: ${pn}" self.ccacheStdenv; }))
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
(self: super: {
|
|
|
|
ccacheWrapper = super.ccacheWrapper.override {
|
|
|
|
extraConfig = ''
|
|
|
|
export CCACHE_COMPRESS=1
|
|
|
|
export CCACHE_DIR="${cfg.cacheDir}"
|
|
|
|
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'"
|
2024-02-29 20:09:43 +00:00
|
|
|
echo " sudo chown ${cfg.owner}:${cfg.group} '$CCACHE_DIR'"
|
2020-04-24 23:36:52 +00:00
|
|
|
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
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
|
|
|
})
|
|
|
|
];
|
2020-08-20 17:08:02 +00:00
|
|
|
}
|