← Back to Pull requests

nixos/docker-rootless: add option to autoprune on schedule #445757

Merged
georgkrause merged 3 files (317 lines) on Dec 05, 2026 00:12 UTC
View on GitHub
georgkrause georgkrause commented on Sep 24, 2025 10:18 UTC
Author

The docker module already has an option to enable an auto-prune systemd service. However, docker rootless did not had this option, yet. When I switched, one of my hosts run full, so I think this is a valuable addition. This is my first contribution, so please let me know if I did something wrong so I can fix it.
<!--
^ Please summarise the changes you have done and explain why they are necessary here ^

For package updates please link to a changelog or describe changes, this helps your fellow maintainers discover breaking updates.
For new packages please briefly describe the package or provide a link to its homepage.
-->

Things done

<!-- Please check what applies. Note that these are not hard requirements but merely serve as information for reviewers. -->

  • Built on platform:

  • - [x] x86_64-linux
    - [ ] aarch64-linux
    - [ ] x86_64-darwin
    - [ ] aarch64-darwin
  • Tested, as applicable:

  • - [ ] [NixOS tests] in [nixos/tests].
    - [ ] [Package tests] at passthru.tests.
    - [ ] Tests in [lib/tests] or [pkgs/test] for functions and "core" functionality.
  • Ran nixpkgs-review on this PR. See [nixpkgs-review usage].

  • Tested basic functionality of all binary files, usually in ./result/bin/.

  • Nixpkgs Release Notes

  • - [ ] Package update: when the change is major or breaking.
  • NixOS Release Notes

  • - [ ] Module addition: when adding a new NixOS module.
    - [ ] Module update: when the change is significant.
  • Fits [CONTRIBUTING.md], [pkgs/README.md], [maintainers/README.md] and other READMEs.
  • [NixOS tests]: https://nixos.org/manual/nixos/unstable/index.html#sec-nixos-tests
    [Package tests]: https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md#package-tests
    [nixpkgs-review usage]: https://github.com/Mic92/nixpkgs-review#usage

    [CONTRIBUTING.md]: https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md
    [lib/tests]: https://github.com/NixOS/nixpkgs/blob/master/lib/tests
    [maintainers/README.md]: https://github.com/NixOS/nixpkgs/blob/master/maintainers/README.md
    [nixos/tests]: https://github.com/NixOS/nixpkgs/blob/master/nixos/tests
    [pkgs/README.md]: https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md
    [pkgs/test]: https://github.com/NixOS/nixpkgs/blob/master/pkgs/test

    ---

    Add a :+1: [reaction] to [pull requests you find important].

    [reaction]: https://github.blog/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/
    [pull requests you find important]: https://github.com/NixOS/nixpkgs/pulls?q=is%3Aopen+sort%3Areactions-%2B1-desc

    Diff: 317 lines (+182 / -135) across 3 files Reviewer: aaravrav Review duration: 628673m
    All Vouch review confidence checks passed
    Residual risk 0.12 = Change Risk (0.40) × (1 − Review Confidence (0.69)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 317 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.47 · 5 substantive comment(s)
    0.47
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    69%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.345
    Real PR Conversation Timeline
    27 items
    georgkrause georgkrause commented on Sep 24, 2025 11:51 UTC
    Author

    Looks like I did quite something wrong, at least not everything went red. Lets see.

    acid-bong acid-bong requested changes on Sep 24, 2025 15:18 UTC
    Changes Requested Reviewer

    please, check out the docs on title conventions (they're the same for both commits and PRs). specifically for modules (and you're editing a NixOS module) the format is nixos/MODULE: CHANGES

    you got the commit right, rename the PR

    acid-bong acid-bong commented on code on Sep 24, 2025 15:21 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -58,6 +58,65 @@ in
             Extra packages to add to PATH for the docker daemon process.
           '';
         };
    +
    +    autoPrune = {
    +      enable = mkOption {

    if you look closely, you'll see that there's no with lib; in this scope. prepend lib. to every mkOption and types that you've added

    acid-bong acid-bong commented on code on Sep 25, 2025 06:24 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -101,6 +160,37 @@ in
             StartLimitBurst = 3;
           };
         };
    +
    +    systemd.user.services.docker-prune = {
    +      description = "Prune docker resources";
    +
    +      restartIfChanged = false;
    +      unitConfig.X-StopOnRemoval = false;
    +
    +      serviceConfig = {
    +        Type = "oneshot";
    +        ExecStart = utils.escapeSystemdExecArgs (
    +          [
    +            (lib.getExe cfg.package)
    +            "system"
    +            "prune"
    +            "-f"
    +          ]
    +          ++ cfg.autoPrune.flags
    +        );
    +      };
    +
    +      startAt = optional cfg.autoPrune.enable cfg.autoPrune.dates;

          startAt = lib.optional cfg.autoPrune.enable cfg.autoPrune.dates;
    

    acid-bong acid-bong commented on code on Sep 25, 2025 06:24 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -101,6 +160,37 @@ in
             StartLimitBurst = 3;
           };
         };
    +
    +    systemd.user.services.docker-prune = {
    +      description = "Prune docker resources";
    +
    +      restartIfChanged = false;
    +      unitConfig.X-StopOnRemoval = false;
    +
    +      serviceConfig = {
    +        Type = "oneshot";
    +        ExecStart = utils.escapeSystemdExecArgs (
    +          [
    +            (lib.getExe cfg.package)
    +            "system"
    +            "prune"
    +            "-f"
    +          ]
    +          ++ cfg.autoPrune.flags
    +        );
    +      };
    +
    +      startAt = optional cfg.autoPrune.enable cfg.autoPrune.dates;
    +      after = [ "docker.service" ];
    +      requires = [ "docker.service" ];
    +    };
    +
    +    systemd.user.timers.docker-prune = mkIf cfg.autoPrune.enable {

        systemd.user.timers.docker-prune = lib.mkIf cfg.autoPrune.enable {
    

    acid-bong acid-bong requested changes on Sep 25, 2025 06:25 UTC
    Changes Requested Reviewer

    forgot a couple more

    georgkrause georgkrause commented on Sep 25, 2025 07:29 UTC
    Author

    @acid-bong Thank you a lot for your help! I am pretty overwhelmed by how this is supposed to work, I tried reading through all guides, but surely missed some points. Also I am not quite sure how to run such a build locally, so I am relying on the CI Jobs, but since I am not yet well trained on reading the logs, it took me a while. Therefore your hints and help is really appreciated!

    Regarding the lib, I am wondering if it wouldn't be better to align the normal docker and docker rootless module? But this might be another pull request for someone with more experience than I have.

    acid-bong acid-bong commented on Sep 25, 2025 07:32 UTC
    Contributor

    I am wondering if it wouldn't be better to align the normal docker and docker rootless module? But this might be another pull request for someone with more experience than I have.

    yeah, it's also unrelated to this PR's scope

    acid-bong acid-bong commented on code on Sep 25, 2025 07:34 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -101,6 +160,37 @@ in
             StartLimitBurst = 3;
           };
         };
    +
    +    systemd.user.services.docker-prune = {
    +      description = "Prune docker resources";
    +
    +      restartIfChanged = false;
    +      unitConfig.X-StopOnRemoval = false;
    +
    +      serviceConfig = {
    +        Type = "oneshot";
    +        ExecStart = utils.escapeSystemdExecArgs (

    add utils to the top args

    acid-bong acid-bong commented on Sep 25, 2025 07:35 UTC
    Contributor

    I am relying on the CI Jobs

    these errors are still detectable with a language server (nil, nixd) or a linter

    georgkrause georgkrause commented on Sep 25, 2025 07:55 UTC
    Author

    @acid-bong Again thank you, I got a language server configured and those stupid mistakes shouldn't happen again. This looks a lot more green right now, great!

    limwa limwa commented on Oct 15, 2025 15:30 UTC
    Contributor

    As @acid-bong mentioned, the correct PR title would be "nixos/docker-rootless: add option to autoprune on schedule". This would also be the title for the commit in the PR.

    Also, one thing I'm noticing is that most of this code is duplicated from the docker.nix module. Is there a way we could maybe reuse some of the code?

    georgkrause georgkrause commented on Dec 15, 2025 08:18 UTC
    Author

    @limwa Thank you for the hints! As I stated above, reusing code feels like the way to go here, however I don't know how to do it. If you give me some pointers, I'd be happy to try, though.

    limwa limwa commented on Dec 28, 2025 19:33 UTC
    Contributor

    @limwa Thank you for the hints! As I stated above, reusing code feels like the way to go here, however I don't know how to do it. If you give me some pointers, I'd be happy to try, though.

    Hi, I asked about that on the NixOS/Nixpkgs Contributions matrix chat, but got no response. I have some clues on how that could be done, but since it would be the first time it is done, as far as I am aware, I think it's okay to leave it as-is.

    georgkrause georgkrause commented on Dec 28, 2025 20:20 UTC
    Author

    > @limwa Thank you for the hints! As I stated above, reusing code feels like the way to go here, however I don't know how to do it. If you give me some pointers, I'd be happy to try, though.

    > Hi, I asked about that on the NixOS/Nixpkgs Contributions matrix chat, but got no response. I have some clues on how that could be done, but since it would be the first time it is done, as far as I am aware, I think it's okay to leave it as-is.

    Thank you for the effort to investigate! :)

    aaravrav aaravrav commented on code on Oct 05, 2026 17:18 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -101,6 +161,37 @@ in
             StartLimitBurst = 3;
           };
         };
    +
    +    systemd.user.services.docker-prune = {
    +      description = "Prune docker resources";
    +

    environment.DOCKER_HOST = "unix://%t/docker.sock";
    

    By default the service is connecting to /var/run/docker.sock because nowhere it is specified to use the rootless socket

    aaravrav aaravrav commented on code on Oct 05, 2026 17:27 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -101,6 +161,37 @@ in
             StartLimitBurst = 3;
           };
         };
    +
    +    systemd.user.services.docker-prune = {
    +      description = "Prune docker resources";
    +
    +      restartIfChanged = false;
    +      unitConfig.X-StopOnRemoval = false;

          unitConfig.ConditionUser = "!root";
          unitConfig.X-StopOnRemoval = false;
    

    The rootless daemon never runs for root but this job doesn't have that restriction. Root also has a user manager

    aaravrav aaravrav commented on code on Oct 05, 2026 17:28 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -101,6 +161,37 @@ in
             StartLimitBurst = 3;
           };
         };
    +
    +    systemd.user.services.docker-prune = {
    +      description = "Prune docker resources";
    +
    +      restartIfChanged = false;
    +      unitConfig.X-StopOnRemoval = false;
    +
    +      serviceConfig = {
    +        Type = "oneshot";
    +        ExecStart = utils.escapeSystemdExecArgs (
    +          [
    +            (lib.getExe cfg.package)
    +            "system"
    +            "prune"
    +            "-f"
    +          ]
    +          ++ cfg.autoPrune.flags
    +        );
    +      };
    +
    +      startAt = lib.optional cfg.autoPrune.enable cfg.autoPrune.dates;
    +      after = [ "docker.service" ];
    +      requires = [ "docker.service" ];
    +    };
    +
    +    systemd.user.timers.docker-prune = lib.mkIf cfg.autoPrune.enable {

        systemd.user.timers.docker-prune = lib.mkIf cfg.autoPrune.enable {
        unitConfig.ConditionUser = "!root";
    

    same as above

    aaravrav aaravrav commented on code on Oct 05, 2026 17:31 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -101,6 +161,37 @@ in
             StartLimitBurst = 3;
           };
         };
    +
    +    systemd.user.services.docker-prune = {
    +      description = "Prune docker resources";
    +

    Have you tested your changes with docker-rootless on your machine?

    georgkrause georgkrause commented on code on Oct 13, 2026 09:20 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -101,6 +161,37 @@ in
             StartLimitBurst = 3;
           };
         };
    +
    +    systemd.user.services.docker-prune = {
    +      description = "Prune docker resources";
    +

    I can remember doing it, but I was even more a beginner than I am now, so lets redo that exercise now!

    georgkrause georgkrause commented on code on Oct 13, 2026 09:32 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -101,6 +161,37 @@ in
             StartLimitBurst = 3;
           };
         };
    +
    +    systemd.user.services.docker-prune = {
    +      description = "Prune docker resources";
    +

    Since I am running into an error that is fixed by that line I probably did not tested it properly back in the day. I am sorry for the overhead I created for you, @aaravrav, by not doing proper testing on my own in the first place. That's usually not my style.

    georgkrause georgkrause commented on Oct 13, 2026 11:03 UTC
    Author

    Thank you @aaravrav for the effort to test this one. I took all your suggestions and tested it on my maschine, where the service successfully cleaned up my docker images just fine. If you don't mind I'd appreciate your approval here! :)

    aaravrav aaravrav approved these changes on Oct 13, 2026 14:25 UTC
    Approved Reviewer

    Approved these changes.

    kakenbok kakenbok approved these changes on Oct 26, 2026 11:24 UTC
    Approved Reviewer

    Approved these changes.

    Stunkymonkey Stunkymonkey commented on code on Nov 16, 2026 21:10 UTC
    Code Review
    nixos/modules/virtualisation/docker-rootless.nix:1
    @@ -58,6 +59,65 @@ in
             Extra packages to add to PATH for the docker daemon process.
           '';
         };
    +
    +    autoPrune = {

    small nit pick:

    The allVolumes option is missing. "docker.nix" has autoPrune.allVolumes.{enable,flags}

    I think this PR copies an older version of the "rootful" options.

    Stunkymonkey Stunkymonkey approved these changes on Dec 03, 2026 21:23 UTC
    Approved Reviewer

    i reordered the commits and squashed one to reduce the amount of changes.

    Reviewers
    Assignees
    georgkrause georgkrause
    Labels
    risk: low confidence: 69.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 40.4% (MEDIUM) · Higher = larger blast radius
    40.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 69.4% · Lower = rushed or shallow review
    69.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 12.4% · Safe to merge
    12.4%
    Repository