1{
2 lib,
3 pkgs,
4 config,
5 utils,
6 ...
7}:
8let
9 cfg = config.services.scx;
10in
11{
12 options.services.scx = {
13 enable = lib.mkEnableOption null // {
14 description = ''
15 Whether to enable SCX service, a daemon to run schedulers from userspace.
16
17 ::: {.note}
18 This service requires a kernel with the Sched-ext feature.
19 Generally, kernel version 6.12 and later are supported.
20 :::
21 '';
22 };
23
24 package = lib.mkOption {
25 type = lib.types.package;
26 default = pkgs.scx.full;
27 defaultText = lib.literalExpression "pkgs.scx.full";
28 example = lib.literalExpression "pkgs.scx.rustscheds";
29 description = ''
30 `scx` package to use. `scx.full`, which includes all schedulers, is the default.
31 You may choose a minimal package, such as `pkgs.scx.rustscheds`.
32
33 ::: {.note}
34 Overriding this does not change the default scheduler; you should set `services.scx.scheduler` for it.
35 :::
36 '';
37 };
38
39 scheduler = lib.mkOption {
40 type = lib.types.enum [
41 "scx_bpfland"
42 "scx_central"
43 "scx_flash"
44 "scx_flatcg"
45 "scx_lavd"
46 "scx_layered"
47 "scx_mitosis"
48 "scx_nest"
49 "scx_pair"
50 "scx_qmap"
51 "scx_rlfifo"
52 "scx_rustland"
53 "scx_rusty"
54 "scx_sdt"
55 "scx_simple"
56 "scx_userland"
57 ];
58 default = "scx_rustland";
59 example = "scx_bpfland";
60 description = ''
61 Which scheduler to use. See [SCX documentation](https://github.com/sched-ext/scx/tree/main/scheds)
62 for details on each scheduler and guidance on selecting the most suitable one.
63 '';
64 };
65
66 extraArgs = lib.mkOption {
67 type = lib.types.listOf lib.types.singleLineStr;
68 default = [ ];
69 example = [
70 "--slice-us 5000"
71 "--verbose"
72 ];
73 description = ''
74 Parameters passed to the chosen scheduler at runtime.
75
76 ::: {.note}
77 Run `chosen-scx-scheduler --help` to see the available options. Generally,
78 each scheduler has its own set of options, and they are incompatible with each other.
79 :::
80 '';
81 };
82 };
83
84 config = lib.mkIf cfg.enable {
85 environment.systemPackages = [ cfg.package ];
86
87 systemd.services.scx = {
88 description = "SCX scheduler daemon";
89
90 # SCX service should be started only if the kernel supports sched-ext
91 unitConfig.ConditionPathIsDirectory = "/sys/kernel/sched_ext";
92
93 startLimitIntervalSec = 30;
94 startLimitBurst = 2;
95
96 serviceConfig = {
97 Type = "simple";
98 ExecStart = utils.escapeSystemdExecArgs (
99 [
100 (lib.getExe' cfg.package cfg.scheduler)
101 ]
102 ++ cfg.extraArgs
103 );
104 Restart = "on-failure";
105 };
106
107 wantedBy = [ "multi-user.target" ];
108 };
109
110 assertions = [
111 {
112 assertion = lib.versionAtLeast config.boot.kernelPackages.kernel.version "6.12";
113 message = "SCX is only supported on kernel version >= 6.12.";
114 }
115 ];
116 };
117
118 meta.maintainers = with lib.maintainers; [ johnrtitor ];
119}