Skip to content

Conversation

nenikitov
Copy link

Motivation

I would like to run garbage collection automatically, but I find flags given to nix-collect-garbage to be imprecise:

  • Deleting by age can either delete too many generations when the system is rebuilt less frequently then the period of garbage collection, or delete too few when the system is rebuilt more frequently.
  • Deleting by amount (is not possible current flags) is more precise, but can delete old rollbacks when the system is built sporadically.

This PR adds 2 new flags, --keep-min and --keep-max:

  • keep-max by itself allows deleting by amount, --keep-max 5 will preserve current generation and 5 most recent old ones
  • keep-min and keep-max both work in conjunction with delete-older-than, working as bounds on how many generations should be preserved.

Context

Solves #9455 and builds upon feedback given to #10426.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added documentation new-cli Relating to the "nix" command labels Sep 17, 2025
@nenikitov nenikitov marked this pull request as draft September 17, 2025 04:08
@nenikitov nenikitov marked this pull request as ready for review September 17, 2025 04:08
@Mic92 Mic92 requested a review from Copilot September 25, 2025 07:28
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds new flags --keep-min and --keep-max to nix-collect-garbage to provide more precise control over generation deletion by allowing trimming both by count and by date.

  • Adds --keep-min and --keep-max flags for count-based generation management
  • Implements a new deleteGenerationsFilter function to handle complex deletion logic with both time and count constraints
  • Updates documentation with examples showing how to combine time and count-based deletion

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/nix/nix-collect-garbage/nix-collect-garbage.cc Adds new command-line flag parsing for --keep-min and --keep-max options
src/libstore/profiles.cc Implements the core deleteGenerationsFilter function and refactors existing deletion functions to use it
src/libstore/include/nix/store/profiles.hh Adds function declaration and comprehensive documentation for the new deletion API
src/libmain/include/nix/main/shared.hh Enhances getIntArg function to support parsing integers without unit suffixes
doc/manual/source/command-ref/nix-collect-garbage.md Updates documentation with new flag descriptions and usage examples
Comments suppressed due to low confidence (1)

src/nix/nix-collect-garbage/nix-collect-garbage.cc:1

  • Missing space before opening brace. Should be } else if (*arg == "--keep-max") { to maintain consistent formatting with other conditional blocks.
#include "nix/util/file-system.hh"

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Member

@Mic92 Mic92 left a comment

Choose a reason for hiding this comment

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

Agree that this is a useful addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation new-cli Relating to the "nix" command
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants