1{ config, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.boot.tmp;
7in
8{
9 imports = [
10 (mkRenamedOptionModule [ "boot" "cleanTmpDir" ] [ "boot" "tmp" "cleanOnBoot" ])
11 (mkRenamedOptionModule [ "boot" "tmpOnTmpfs" ] [ "boot" "tmp" "useTmpfs" ])
12 (mkRenamedOptionModule [ "boot" "tmpOnTmpfsSize" ] [ "boot" "tmp" "tmpfsSize" ])
13 ];
14
15 options = {
16 boot.tmp = {
17 cleanOnBoot = mkOption {
18 type = types.bool;
19 default = false;
20 description = ''
21 Whether to delete all files in {file}`/tmp` during boot.
22 '';
23 };
24
25 tmpfsSize = mkOption {
26 type = types.oneOf [ types.str types.types.ints.positive ];
27 default = "50%";
28 description = ''
29 Size of tmpfs in percentage.
30 Percentage is defined by systemd.
31 '';
32 };
33
34 useTmpfs = mkOption {
35 type = types.bool;
36 default = false;
37 description = ''
38 Whether to mount a tmpfs on {file}`/tmp` during boot.
39
40 ::: {.note}
41 Large Nix builds can fail if the mounted tmpfs is not large enough.
42 In such a case either increase the tmpfsSize or disable this option.
43 :::
44 '';
45 };
46 };
47 };
48
49 config = {
50 # When changing remember to update /tmp mount in virtualisation/qemu-vm.nix
51 systemd.mounts = mkIf cfg.useTmpfs [
52 {
53 what = "tmpfs";
54 where = "/tmp";
55 type = "tmpfs";
56 mountConfig.Options = concatStringsSep "," [
57 "mode=1777"
58 "strictatime"
59 "rw"
60 "nosuid"
61 "nodev"
62 "size=${toString cfg.tmpfsSize}"
63 ];
64 }
65 ];
66
67 systemd.tmpfiles.rules = optional cfg.cleanOnBoot "D! /tmp 1777 root root";
68 };
69}