From e2299a57d260cfd8c37d3ad4410b41efcdd76817 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sun, 20 Jul 2025 19:17:08 +0900 Subject: [PATCH] cmd/nix-tool: flag for idle priority Mainly for usability. This is often ran on a workstation. --- cmd/nix-tool/main.go | 10 ++++++++++ cmd/nix-tool/sched.go | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 cmd/nix-tool/sched.go diff --git a/cmd/nix-tool/main.go b/cmd/nix-tool/main.go index dc8cc1c..1e2ca7e 100644 --- a/cmd/nix-tool/main.go +++ b/cmd/nix-tool/main.go @@ -9,6 +9,7 @@ import ( "log" "os" "os/signal" + "runtime" "slices" "strings" "syscall" @@ -29,6 +30,7 @@ func main() { var ( flagStore string + flagIdle bool flagNixOS bool flagVerbose bool flagJSON bool @@ -36,6 +38,13 @@ func main() { c := command.New(os.Stderr, log.Printf, "nix-tool", func(args []string) error { log.SetFlags(0) + if flagIdle { + runtime.LockOSThread() + if err := schedSetPolicy(0, SCHED_IDLE); err != nil { + return commandHandlerError(fmt.Sprintf("cannot set policy: %v", err)) + } + } + var extraArgs []string flagStore = strings.TrimSpace(flagStore) if flagStore != string(os.PathSeparator) { @@ -58,6 +67,7 @@ func main() { return nil }). Flag(&flagStore, "store", command.StringFlag("/"), "Path to the nix root"). + Flag(&flagIdle, "idle", command.BoolFlag(false), "Whether to set SCHED_IDLE policy"). Flag(&flagNixOS, "nixos", command.BoolFlag(false), "Interpret input as NixOS flake installable"). Flag(&flagVerbose, "v", command.BoolFlag(false), "Connect nix stderr"). Flag(&flagJSON, "json", command.BoolFlag(false), "Serialise output in JSON when applicable") diff --git a/cmd/nix-tool/sched.go b/cmd/nix-tool/sched.go new file mode 100644 index 0000000..36d750e --- /dev/null +++ b/cmd/nix-tool/sched.go @@ -0,0 +1,25 @@ +package main + +/* +#ifndef _GNU_SOURCE +#define _GNU_SOURCE // SCHED_IDLE +#endif + +#include + +const struct sched_param param = { .sched_priority = 0 }; +const int NIX_TOOL_SCHED_IDLE = SCHED_IDLE; +*/ +import "C" + +var ( + SCHED_IDLE = int(C.NIX_TOOL_SCHED_IDLE) +) + +func schedSetPolicy(pid, policy int) error { + r, err := C.sched_setscheduler(C.pid_t(pid), C.int(policy), &C.param) + if r != 0 { + return err + } + return nil +}