Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/config/sysfs: init module #391329

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

illdefined
Copy link
Contributor

@illdefined illdefined commented Mar 19, 2025

This module introduces a config option boot.kernel.sysfs, which permits setting of sysfs parameters.

The configuration option accepts a nested attribute set of sysfs path components with arbitrary values, for example:

{
  kernel.mm.transparent_hugepage = {
    enabled = "always";
    defrag = "defer";
    shmem_enabled = "within_size";
  };
}

The options will be applied through systemd path units watching the individual files or glob patterns and writing the configured value through a small service using systemd-tmpfiles once they exist.

The supplied NixOS test checks if configured parameters are actually applied.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 6.topic: systemd 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Mar 19, 2025
@illdefined illdefined force-pushed the nixos-config-sysfs branch 3 times, most recently from 09c76cf to c1a036b Compare March 19, 2025 18:04
@illdefined
Copy link
Contributor Author

The NixOS manual build failure appears to be unrelated to this change.

I am able to build the manual locally without any issues:
dpc1yk271wl343axkdy7q3h86jx815m4-nixos-manual-html.tar.gz

@illdefined illdefined force-pushed the nixos-config-sysfs branch 4 times, most recently from 4919977 to cd2a1b7 Compare March 19, 2025 19:17
@github-actions github-actions bot added 8.has: changelog 8.has: documentation This PR adds or changes documentation labels Mar 19, 2025
@illdefined illdefined force-pushed the nixos-config-sysfs branch 3 times, most recently from 15061d8 to 8461f59 Compare March 20, 2025 00:15
@illdefined illdefined marked this pull request as ready for review March 20, 2025 00:38
@illdefined illdefined changed the title nixos/config/sysfs: init nixos/config/sysfs: init module Mar 20, 2025
@illdefined illdefined force-pushed the nixos-config-sysfs branch 2 times, most recently from 4f517c7 to ebfe568 Compare March 20, 2025 04:03
@r-vdp
Copy link
Contributor

r-vdp commented Mar 20, 2025

One issue here, is that a lot of sysfs paths depend on kernel modules being loaded, which may happen at a later time.
On my systems, I use systemd path units to trigger a service whenever a certain path appears (because a module got loaded, e.g. hotplugging a device), and then that service writes a value to the file in sysfs.

I wonder if we'd want to support something like that? Currently the module in this PR will only work for sysfs paths that are present at the time systemd-tmpfiles runs.

boot.kernel.sysfs = lib.mkOption {
type = lib.types.submodule {
freeformType = lib.types.attrsOf sysfsAttrs // {
description = "nested attribute set of null or sysfs attribute values";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types have descriptions? That's new to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description is usually generated automatically, but due to the nesting this would lead to an infinite recursion. I therefore override the description attribute and describe the type manually.

This description is included in the generated option documentation.

};
};

description = ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if that will display properly on search.nxios.org because of its length.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a conventional upper limit on the description length?

I could perhaps elaborate on the option in a section of the NixOS manual and refer to it in this description.

@SuperSandro2000
Copy link
Member

One issue here, is that a lot of sysfs paths depend on kernel modules being loaded, which may happen at a later time.

sysctl has the same problem. I usually work around it by loading the kernel module already in the initrd.

@illdefined
Copy link
Contributor Author

One issue here, is that a lot of sysfs paths depend on kernel modules being loaded, which may happen at a later time.

sysctl has the same problem. I usually work around it by loading the kernel module already in the initrd.

Instead of relying on systemd-tmpfiles, I could create individual path units for every entry.

@illdefined
Copy link
Contributor Author

sysctl has the same problem. I usually work around it by loading the kernel module already in the initrd.

Instead of relying on systemd-tmpfiles, I could create individual path units for every entry.

I have come up with a mostly working solution based on systemd.path, but I have yet to figure out a way to escape glob patterns for use in a shell script, without having to write a parser in Nix.

@illdefined
Copy link
Contributor Author

Unable to find an easy way to escape glob patterns for use in a shell script, I decided to just spawn an individual instance of systemd-tmpfiles for every path (pattern).

@h7x4 h7x4 added 8.has: module (new) This PR adds a module in `nixos/` 8.has: tests This PR has tests labels Mar 22, 2025
@illdefined
Copy link
Contributor Author

I reduced the number of individual services by using unit templates and instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog 8.has: documentation This PR adds or changes documentation 8.has: module (new) This PR adds a module in `nixos/` 8.has: module (update) This PR changes an existing module in `nixos/` 8.has: tests This PR has tests 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants