1{ config, lib, ... }:
2let
3 cfg = config.boot.tmp;
4in
5{
6 imports = [
7 (lib.mkRenamedOptionModule [ "boot" "cleanTmpDir" ] [ "boot" "tmp" "cleanOnBoot" ])
8 (lib.mkRenamedOptionModule [ "boot" "tmpOnTmpfs" ] [ "boot" "tmp" "useTmpfs" ])
9 (lib.mkRenamedOptionModule [ "boot" "tmpOnTmpfsSize" ] [ "boot" "tmp" "tmpfsSize" ])
10 ];
11
12 options = {
13 boot.tmp = {
14 cleanOnBoot = lib.mkOption {
15 type = lib.types.bool;
16 default = false;
17 description = ''
18 Whether to delete all files in {file}`/tmp` during boot.
19 '';
20 };
21
22 tmpfsSize = lib.mkOption {
23 type = lib.types.oneOf [
24 lib.types.str
25 lib.types.ints.positive
26 ];
27 default = "50%";
28 description = ''
29 Size of tmpfs in percentage.
30 Percentage is defined by systemd.
31 '';
32 };
33
34 tmpfsHugeMemoryPages = lib.mkOption {
35 type = lib.types.enum [
36 "never"
37 "always"
38 "within_size"
39 "advise"
40 ];
41 default = "never";
42 example = "within_size";
43 description = ''
44 never - Do not allocate huge memory pages. This is the default.
45 always - Attempt to allocate huge memory page every time a new page is needed.
46 within_size - Only allocate huge memory pages if it will be fully within i_size. Also respect madvise(2) hints. Recommended.
47 advise - Only allocate huge memory pages if requested with madvise(2).
48 '';
49 };
50
51 useTmpfs = lib.mkOption {
52 type = lib.types.bool;
53 default = false;
54 description = ''
55 Whether to mount a tmpfs on {file}`/tmp` during boot.
56
57 ::: {.note}
58 Large Nix builds can fail if the mounted tmpfs is not large enough.
59 In such a case either increase the tmpfsSize or disable this option.
60 :::
61 '';
62 };
63 };
64 };
65
66 config = {
67 # When changing remember to update /tmp mount in virtualisation/qemu-vm.nix
68 systemd.mounts = lib.mkIf cfg.useTmpfs [
69 {
70 what = "tmpfs";
71 where = "/tmp";
72 type = "tmpfs";
73 mountConfig.Options = lib.concatStringsSep "," [
74 "mode=1777"
75 "strictatime"
76 "rw"
77 "nosuid"
78 "nodev"
79 "size=${toString cfg.tmpfsSize}"
80 "huge=${cfg.tmpfsHugeMemoryPages}"
81 ];
82 }
83 ];
84
85 systemd.tmpfiles.rules = lib.optional cfg.cleanOnBoot "D! /tmp 1777 root root";
86 };
87}