nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix

nixos-rebuild: Add list-generations

Add new command `nixos-rebuild list-generations`. It will show an output
like

```
$ nixos-rebuild list-generations
Generation Build-date NixOS version Kernel Configuration Revision Specialisations
52 (current) Fri 2023-08-18 08:17:27 23.11.20230817.0f46300 6.4.10 448160aeccf6a7184bd8a84290d527819f1c552c *
51 Mon 2023-08-07 17:56:41 23.11.20230807.31b1eed 6.4.8 99ef480007ca51e3d440aa4fa6558178d63f9c42 *
```

This also mentions the change in the upcoming release notes

+100 -3
+2
nixos/doc/manual/release-notes/rl-2311.section.md
··· 8 8 9 9 - LXD now supports virtual machine instances to complement the existing container support 10 10 11 + - The `nixos-rebuild` command has been given a `list-generations` subcommand. See `man nixos-rebuild` for more details. 12 + 11 13 ## New Services {#sec-release-23.11-new-services} 12 14 13 15 - [MCHPRS](https://github.com/MCHPR/MCHPRS), a multithreaded Minecraft server built for redstone. Available as [services.mchprs](#opt-services.mchprs.enable).
+3 -1
pkgs/os-specific/linux/nixos-rebuild/default.nix
··· 3 3 , coreutils 4 4 , gnused 5 5 , gnugrep 6 + , jq 7 + , util-linux 6 8 , nix 7 9 , lib 8 10 , nixosTests ··· 22 20 nix_x86_64_linux = fallback.x86_64-linux; 23 21 nix_i686_linux = fallback.i686-linux; 24 22 nix_aarch64_linux = fallback.aarch64-linux; 25 - path = lib.makeBinPath [ coreutils gnused gnugrep ]; 23 + path = lib.makeBinPath [ coreutils gnused gnugrep jq util-linux ]; 26 24 nativeBuildInputs = [ 27 25 installShellFiles 28 26 ];
+9 -1
pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.8
··· 10 10 .Sh SYNOPSIS 11 11 .Nm 12 12 .Bro 13 - .Cm switch | boot | test | build | dry-build | dry-activate | edit | build-vm | build-vm-with-bootloader 13 + .Cm switch | boot | test | build | dry-build | dry-activate | edit | build-vm | build-vm-with-bootloader | list-generations Op Fl -json 14 14 .Brc 15 15 .br 16 16 .Op Fl -upgrade | -upgrade-all ··· 196 196 containing a 197 197 .Pa /boot 198 198 partition. 199 + . 200 + .It Cm list-generations Op Fl -json 201 + List the available generations in a similar manner to the boot loader 202 + menu. It shows the generation number, build date and time, NixOS version, 203 + kernel version and the configuration revision. This is useful to get 204 + information e.g. for which generation to roll back to with 205 + .Ic nixos-rebuild switch Fl -generation Ar N 206 + There is also a json version of output available. 199 207 .El 200 208 . 201 209 .
+86 -1
pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh
··· 36 36 noFlake= 37 37 # comma separated list of vars to preserve when using sudo 38 38 preservedSudoVars=NIXOS_INSTALL_BOOTLOADER 39 + json= 39 40 40 41 # log the given argument to stderr 41 42 log() { ··· 49 48 --help) 50 49 showSyntax 51 50 ;; 52 - switch|boot|test|build|edit|dry-build|dry-run|dry-activate|build-vm|build-vm-with-bootloader) 51 + switch|boot|test|build|edit|dry-build|dry-run|dry-activate|build-vm|build-vm-with-bootloader|list-generations) 53 52 if [ "$i" = dry-run ]; then i=dry-build; fi 54 53 # exactly one action mandatory, bail out if multiple are given 55 54 if [ -n "$action" ]; then showSyntax; fi ··· 146 145 j="$1"; shift 1 147 146 k="$1"; shift 1 148 147 lockFlags+=("$i" "$j" "$k") 148 + ;; 149 + --json) 150 + json=1 149 151 ;; 150 152 *) 151 153 log "$0: unknown option \`$i'" ··· 509 505 510 506 if [ "$action" = dry-build ]; then 511 507 extraBuildFlags+=(--dry-run) 508 + fi 509 + 510 + if [ "$action" = list-generations ]; then 511 + if [ ! -L "$profile" ]; then 512 + log "No profile \`$(basename "$profile")' found" 513 + exit 1 514 + fi 515 + 516 + generation_from_dir() { 517 + generation_dir="$1" 518 + generation_base="$(basename "$generation_dir")" # Has the format "system-123-link" for generation 123 519 + no_link_gen="${generation_base%-link}" # remove the "-link" 520 + echo "${no_link_gen##*-}" # remove everything before the last dash 521 + } 522 + describe_generation(){ 523 + generation_dir="$1" 524 + generation_number="$(generation_from_dir "$generation_dir")" 525 + nixos_version="$(cat "$generation_dir/nixos-version" 2> /dev/null || echo "Unknown")" 526 + 527 + kernel_dir="$(dirname "$(realpath "$generation_dir/kernel")")" 528 + kernel_version="$(ls "$kernel_dir/lib/modules" || echo "Unknown")" 529 + 530 + configurationRevision="$("$generation_dir/sw/bin/nixos-version" --configuration-revision 2> /dev/null || true)" 531 + 532 + # Old nixos-version output ignored unknown flags and just printed the version 533 + # therefore the following workaround is done not to show the default output 534 + nixos_version_default="$("$generation_dir/sw/bin/nixos-version")" 535 + if [ "$configurationRevision" == "$nixos_version_default" ]; then 536 + configurationRevision="" 537 + fi 538 + 539 + # jq automatically quotes the output => don't try to quote it in output! 540 + build_date="$(stat "$generation_dir" --format=%W | jq 'todate')" 541 + 542 + pushd "$generation_dir/specialisation/" > /dev/null || : 543 + specialisation_list=(*) 544 + popd > /dev/null || : 545 + 546 + specialisations="$(jq --compact-output --null-input '$ARGS.positional' --args -- "${specialisation_list[@]}")" 547 + 548 + if [ "$(basename "$generation_dir")" = "$(readlink "$profile")" ]; then 549 + current_generation_tag="true" 550 + else 551 + current_generation_tag="false" 552 + fi 553 + 554 + # Escape userdefined strings 555 + nixos_version="$(jq -aR <<< "$nixos_version")" 556 + kernel_version="$(jq -aR <<< "$kernel_version")" 557 + configurationRevision="$(jq -aR <<< "$configurationRevision")" 558 + cat << EOF 559 + { 560 + "generation": $generation_number, 561 + "date": $build_date, 562 + "nixosVersion": $nixos_version, 563 + "kernelVersion": $kernel_version, 564 + "configurationRevision": $configurationRevision, 565 + "specialisations": $specialisations, 566 + "current": $current_generation_tag 567 + } 568 + EOF 569 + } 570 + 571 + find "$(dirname "$profile")" -regex "$profile-[0-9]+-link" | 572 + sort -Vr | 573 + while read -r generation_dir; do 574 + describe_generation "$generation_dir" 575 + done | 576 + if [ -z "$json" ]; then 577 + jq --slurp -r '.[] | [ 578 + ([.generation, (if .current == true then "current" else "" end)] | join(" ")), 579 + (.date | fromdate | strflocaltime("%Y-%m-%d %H:%M:%S")), 580 + .nixosVersion, .kernelVersion, .configurationRevision, 581 + (.specialisations | join(" ")) 582 + ] | @tsv' | 583 + column --separator $'\t' --table --table-columns "Generation,Build-date,NixOS version,Kernel,Configuration Revision,Specialisation" | 584 + ${PAGER:cat} 585 + else 586 + jq --slurp . 587 + fi 588 + exit 0 512 589 fi 513 590 514 591