lol

Merge pull request #250501 from miallo/nixos-rebuild/list-generations

nixos-rebuild: Add list-generations

authored by

Doron Behar and committed by
GitHub
cfb77410 8251da23

+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 ··· 20 22 nix_x86_64_linux = fallback.x86_64-linux; 21 23 nix_i686_linux = fallback.i686-linux; 22 24 nix_aarch64_linux = fallback.aarch64-linux; 23 - path = lib.makeBinPath [ coreutils gnused gnugrep ]; 25 + path = lib.makeBinPath [ coreutils gnused gnugrep jq util-linux ]; 24 26 nativeBuildInputs = [ 25 27 installShellFiles 26 28 ];
+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() { ··· 48 49 --help) 49 50 showSyntax 50 51 ;; 51 - switch|boot|test|build|edit|dry-build|dry-run|dry-activate|build-vm|build-vm-with-bootloader) 52 + switch|boot|test|build|edit|dry-build|dry-run|dry-activate|build-vm|build-vm-with-bootloader|list-generations) 52 53 if [ "$i" = dry-run ]; then i=dry-build; fi 53 54 # exactly one action mandatory, bail out if multiple are given 54 55 if [ -n "$action" ]; then showSyntax; fi ··· 145 146 j="$1"; shift 1 146 147 k="$1"; shift 1 147 148 lockFlags+=("$i" "$j" "$k") 149 + ;; 150 + --json) 151 + json=1 148 152 ;; 149 153 *) 150 154 log "$0: unknown option \`$i'" ··· 505 509 506 510 if [ "$action" = dry-build ]; then 507 511 extraBuildFlags+=(--dry-run) 512 + fi 513 + 514 + if [ "$action" = list-generations ]; then 515 + if [ ! -L "$profile" ]; then 516 + log "No profile \`$(basename "$profile")' found" 517 + exit 1 518 + fi 519 + 520 + generation_from_dir() { 521 + generation_dir="$1" 522 + generation_base="$(basename "$generation_dir")" # Has the format "system-123-link" for generation 123 523 + no_link_gen="${generation_base%-link}" # remove the "-link" 524 + echo "${no_link_gen##*-}" # remove everything before the last dash 525 + } 526 + describe_generation(){ 527 + generation_dir="$1" 528 + generation_number="$(generation_from_dir "$generation_dir")" 529 + nixos_version="$(cat "$generation_dir/nixos-version" 2> /dev/null || echo "Unknown")" 530 + 531 + kernel_dir="$(dirname "$(realpath "$generation_dir/kernel")")" 532 + kernel_version="$(ls "$kernel_dir/lib/modules" || echo "Unknown")" 533 + 534 + configurationRevision="$("$generation_dir/sw/bin/nixos-version" --configuration-revision 2> /dev/null || true)" 535 + 536 + # Old nixos-version output ignored unknown flags and just printed the version 537 + # therefore the following workaround is done not to show the default output 538 + nixos_version_default="$("$generation_dir/sw/bin/nixos-version")" 539 + if [ "$configurationRevision" == "$nixos_version_default" ]; then 540 + configurationRevision="" 541 + fi 542 + 543 + # jq automatically quotes the output => don't try to quote it in output! 544 + build_date="$(stat "$generation_dir" --format=%W | jq 'todate')" 545 + 546 + pushd "$generation_dir/specialisation/" > /dev/null || : 547 + specialisation_list=(*) 548 + popd > /dev/null || : 549 + 550 + specialisations="$(jq --compact-output --null-input '$ARGS.positional' --args -- "${specialisation_list[@]}")" 551 + 552 + if [ "$(basename "$generation_dir")" = "$(readlink "$profile")" ]; then 553 + current_generation_tag="true" 554 + else 555 + current_generation_tag="false" 556 + fi 557 + 558 + # Escape userdefined strings 559 + nixos_version="$(jq -aR <<< "$nixos_version")" 560 + kernel_version="$(jq -aR <<< "$kernel_version")" 561 + configurationRevision="$(jq -aR <<< "$configurationRevision")" 562 + cat << EOF 563 + { 564 + "generation": $generation_number, 565 + "date": $build_date, 566 + "nixosVersion": $nixos_version, 567 + "kernelVersion": $kernel_version, 568 + "configurationRevision": $configurationRevision, 569 + "specialisations": $specialisations, 570 + "current": $current_generation_tag 571 + } 572 + EOF 573 + } 574 + 575 + find "$(dirname "$profile")" -regex "$profile-[0-9]+-link" | 576 + sort -Vr | 577 + while read -r generation_dir; do 578 + describe_generation "$generation_dir" 579 + done | 580 + if [ -z "$json" ]; then 581 + jq --slurp -r '.[] | [ 582 + ([.generation, (if .current == true then "current" else "" end)] | join(" ")), 583 + (.date | fromdate | strflocaltime("%Y-%m-%d %H:%M:%S")), 584 + .nixosVersion, .kernelVersion, .configurationRevision, 585 + (.specialisations | join(" ")) 586 + ] | @tsv' | 587 + column --separator $'\t' --table --table-columns "Generation,Build-date,NixOS version,Kernel,Configuration Revision,Specialisation" | 588 + ${PAGER:cat} 589 + else 590 + jq --slurp . 591 + fi 592 + exit 0 508 593 fi 509 594 510 595