lf- / flakey-profile

Declarative profiles with nix flakes
63 stars 2 forks source link

Add a `list` and `history` commands #5

Open iFreilicht opened 1 month ago

iFreilicht commented 1 month ago

As this is not intending to be compatible with nix profile list (and rightfully so), maybe it could replace it instead?

This is based on an experiment I did today, and I quite like the result:

{
  description = "Default packages to install into user environment";

  inputs = {
    flakey-profile.url = "github:lf-/flakey-profile";
    nixpkgs.url = "github:NixOS/nixpkgs/3281bec7174f679eabf584591e75979a258d8c40";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, flakey-profile, nixpkgs, flake-utils }@inputs:
    (flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
      in
      {
        packages = {
          profile = flakey-profile.lib.mkProfile
            {
              inherit pkgs;
              paths = with pkgs; [
                coreutils
                gnused
              ];
              pinned = {
                nixpkgs = toString nixpkgs;
              };
            } // (
            let
              empty-profile = pkgs.runCommand "empty" { } "mkdir -p $out";
            in
            {
              history =
                pkgs.writeShellScriptBin "history" ''
                  # Workaround for https://github.com/NixOS/nix/issues/1807
                  ln -snf ${empty-profile} "$(dirname $(readlink ~/.nix-profile))/profile-0-link"
                  nix profile diff-closures | grep -v "^empty:"
                '';
              list = pkgs.writeShellScriptBin "list" ''
                nix store diff-closures ${empty-profile} ~/.nix-profile | grep -v "^empty:"
              '';
            }
          );
        };
      })
    )
  ;
}

After running nix run .#profile.switch, you can easily see which packages are installed:

$ nix run .#profile.list
coreutils: ∅ → 9.5, +1533.7 KiB
flake: ∅ → ε, +27.7 KiB
gmp-with-cxx: ∅ → 6.3.0, +555.1 KiB
gnused: ∅ → 4.9, +246.8 KiB
libcxx: ∅ → 16.0.6, +3161.2 KiB

And, you can also see a history after adding and removing packages:

nix run .#profile.history
Version 0 -> 1:
  coreutils: ∅ → 9.5, +1533.7 KiB
  empty: ε → ∅
  flake: ∅ → ε, +27.7 KiB
  gmp-with-cxx: ∅ → 6.3.0, +555.1 KiB
  gnused: ∅ → 4.9, +246.8 KiB
  libcxx: ∅ → 16.0.6, +3161.2 KiB

Version 1 -> 2:
  jq: ∅ → 1.7.1, +553.5 KiB
  oniguruma: ∅ → 6.9.9, +648.5 KiB

Version 2 -> 3:
  bash: ∅ → 5.2p26, +2782.2 KiB
  bzip2: ∅ → 1.0.8, +106.7 KiB
  expat: ∅ → 2.6.2, +274.0 KiB
  gdbm: ∅ → 1.23, +706.1 KiB
  gnused: 4.9 → ∅, -246.8 KiB
  libffi: ∅ → 3.4.6, +119.8 KiB
  libiconv: ∅ → 99, +44803.6 KiB
  libxcrypt: ∅ → 4.4.36, +160.9 KiB
  mailcap: ∅ → 2.1.53, +109.4 KiB
  ncurses: ∅ → 6.4, +3681.0 KiB
  nh: ∅ → 3.5.15, +9525.3 KiB
  nix-output-monitor: ∅ → 2.1.2, +46719.7 KiB
  nvd: ∅ → 0.2.3, +30.9 KiB
  openssl: ∅ → 3.0.13, +4450.0 KiB
  python3: ∅ → 3.11.9, +89771.8 KiB
  readline: ∅ → 8.2p10, +434.1 KiB
  sqlite: ∅ → 3.45.3, +1673.0 KiB
  tzdata: ∅ → 2024a, +2078.0 KiB
  xz: ∅ → 5.4.6, +233.1 KiB
  zlib: ∅ → 1.3.1, +366.8 KiB

Version 3 -> 4:
  libiconv: 99 → ∅, -44803.6 KiB
  nh: 3.5.15 → ∅, -9525.3 KiB
  nix-output-monitor: 2.1.2 → ∅, -46719.7 KiB

Integrating this would require the addition of a profile argument, as it's not guaranteed that ~/.nix-profile points to the profile you're interested in.

I'd be happy to write a PR, but I'm uncertain if this is desirable at all? Maybe you consider it feature creep?

Additionally, the reliance on two currently unstable nix commands is probably not great. An alternative would be to use nvd instead, which would also produce nicer output, but that could be considered bloat, as it adds python3 as a dependency.

Let me know what you think.