Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
self: super:
{
# Install overlay:
# $ mkdir -p ~/.config/nixpkgs/overlays
# $ curl https://gist.githubusercontent.com/LnL7/570349866bb69467d0caf5cb175faa74/raw/3f3d53fe8e8713ee321ee894ecf76edbcb0b3711/lnl-overlay.nix -o ~/.config/nixpkgs/overlays/lnl.nix
userPackages = super.userPackages or {} // {
# Example:
hello = self.hello;
# add more packages here...
# Default packages:
# cacert = self.cacert;
# nix = self.nix; # don't enable this on multi-user.
nix-rebuild = super.writeScriptBin "nix-rebuild" ''
#!${super.stdenv.shell}
if ! command -v nix-env &>/dev/null; then
echo "warning: nix-env was not found in PATH, add nix to userPackages" >&2
PATH=${self.nix}/bin:$PATH
fi
exec nix-env -f '<nixpkgs>' -r -iA userPackages "$@"
'';
};
}
@efx

This comment has been minimized.

Copy link

efx commented Oct 13, 2017

on macOS, if you haven't installed overlays before, you need to create the directory:

$ mkdir -p ~/.config/nixpkgs/overlays
@grahamc

This comment has been minimized.

Copy link

grahamc commented May 23, 2018

Aftr adding this overlay, run:

nix-env -f '<nixpkgs>' -r -iA userPackages

and from then on you edit this file and run nix-rebuild.

@jcrben

This comment has been minimized.

Copy link

jcrben commented Jul 23, 2018

UPDATE: looks like userPackages isn't showing up in a search but other overlay packages are - I tried giving it a name but that didn't fix it. Guess it's missing some attributes.

Is it expected that this will show up in a search? Been fiddling around for a while trying to get it (mentioned more at NixOS/nixpkgs#43266 (comment)).

@LnL7

This comment has been minimized.

Copy link
Owner Author

LnL7 commented Aug 11, 2018

Use super.userPackages or {} // super.recurseIntoAttrs { ... } to expose these when searching packages, etc.

@jabranham

This comment has been minimized.

Copy link

jabranham commented Dec 11, 2018

This doesn't seem to make info manuals of those programs available.

@lilyball

This comment has been minimized.

Copy link

lilyball commented May 31, 2019

I tweaked this to print out the difference in packages on every rebuild:

self: super:

{
  userPackages = super.userPackages or {} // {
    # My packages
    inherit (self) hello;
    # add more packages here...

    # Default packages
    inherit (self)
      cacert
      nix; # don't enable nix on multi-user
    
    nix-rebuild = super.writeScriptBin "nix-rebuild" ''
      #!${super.stdenv.shell}
      set -e
      if ! command -v nix-env &>/dev/null; then
        echo "warning: nix-env was not found in PATH, add nix to userPackages" >&2
        PATH=${self.nix}/bin:$PATH
      fi
      IFS=- read -r _ oldGen _ <<<"$(readlink "$(readlink ~/.nix-profile)")"
      oldVersions=$(readlink ~/.nix-profile/package_versions || echo "/dev/null")
      nix-env -f '<nixpkgs>' -r -iA userPackages "$@"
      IFS=- read -r _ newGen _ <<<"$(readlink "$(readlink ~/.nix-profile)")"
      ${self.diffutils}/bin/diff --color -u --label "generation $oldGen" $oldVersions \
        --label "generation $newGen" ~/.nix-profile/package_versions \
        || true
    '';

    packageVersions =
      let
        versions = super.lib.attrsets.mapAttrsToList (_: pkg: pkg.name) self.userPackages;
        versionText = super.lib.strings.concatMapStrings (s: s+"\n") versions;
      in
      super.writeTextDir "package_versions" versionText;
  };
}

This dumps all the user packages into a file ~/.nix-profile/package_versions and then diffs this across the install. The output looks like

--- generation 68
+++ generation 69
@@ -33,6 +33,7 @@
 rlwrap-0.43
 ShellCheck-0.6.0
 taskwarrior-2.5.1
+tig-2.4.1
 tmux-2.9a
 unison-2.51.2
 vit-1.3
@steshaw

This comment has been minimized.

Copy link

steshaw commented Jun 12, 2019

Nice, @lilyball 😄

@lilyball

This comment has been minimized.

Copy link

lilyball commented Jun 12, 2019

I just updated my comment because I realized that the diff command was exiting with status code 1 when there's a difference. The update just tacks on || true so the whole rebuild doesn't exit with status code 1.

@steshaw

This comment has been minimized.

Copy link

steshaw commented Jun 12, 2019

Thanks again, @lilyball. I should have noticed:

image

@lilyball

This comment has been minimized.

Copy link

lilyball commented Jul 11, 2020

Here's the version I've been using lately:

self: super:

{
  userPackages = super.userPackages or {}
    // import ./packages.nix self super
    // super.lib.optionalAttrs (builtins.pathExists ./local.nix) (import ./local.nix self super)
    // {
      # Default packages for single-user; don't include this for multi-user
      inherit (self) cacert nix;
    }
    // {
      # Utilities
      nix-rebuild = super.writeShellScriptBin "nix-rebuild" ''
        set -e
        if ! command -v nix-env &>/dev/null; then
          echo "warning: nix-env was not found in PATH, add nix to userPackages" >&2
          PATH=${self.nix}/bin:$PATH
        fi
        IFS=- read -r _ oldGen _ <<<"$(readlink "$(readlink ~/.nix-profile)")"
        oldVersions=$(readlink ~/.nix-profile/package_versions || echo "/dev/null")
        nix-env -f '<nixpkgs>' -r -iA userPackages "$@"
        IFS=- read -r _ newGen _ <<<"$(readlink "$(readlink ~/.nix-profile)")"
        ${self.diffutils}/bin/diff --color -u --label "generation $oldGen" $oldVersions \
          --label "generation $newGen" ~/.nix-profile/package_versions \
          || true
      '';
      nix-what-rebuild = super.writeShellScriptBin "nix-what-rebuild" ''
        set -e
        if ! command -v nix-env &>/dev/null; then
          echo "warning: nix-env was not found in PATH, add nix to userPackages" >&2
          PATH=${self.nix}/bin:$PATH
        fi
        IFS=- read -r _ oldGen _ <<<"$(readlink "$(readlink ~/.nix-profile)")"
        oldVersions=$(readlink ~/.nix-profile/package_versions || echo "/dev/null")
        newVersions=$(nix-build --no-out-link -A userPackages.packageVersions '<nixpkgs>')
        ${self.diffutils}/bin/diff --color -u --label "generation $oldGen" "$oldVersions" \
          --label "after rebuild" "$newVersions" \
          && echo "no changes" \
          || true
      '';

      packageVersions =
        let
          collect = attrs:
            let recurse = x:
              if super.lib.isDerivation x then [x]
              else if x.recurseForDerivations or false then collect x
              else [];
            in super.lib.concatMap recurse (super.lib.attrValues attrs);
          versions = map (pkg: pkg.name) (collect self.userPackages);
          versionText = super.lib.strings.concatMapStrings (s: s+"\n") versions;
        in
        super.writeTextDir "package_versions" versionText;
  };
}

With this version my packages are stored in a sibling file called packages.nix, which looks like

self: super:
# Packages exposed as part of nixpkgs.userPackages
{
  inherit (self)
    fish tmux
    # …
    ;
}

and I can optionally add a separate local.nix that looks the same. The reason for this is I sync this overlay between machines, and this way I can exclude local.nix from the syncing in order to get per-machine customization.

Besides that, the differences between this and my previous version are the nix-what-rebuild command that prints the diff that nix-rebuild will print if I run it, and it fixes the package version tracking to handle children of userPackages being package sets (which installs the whole set if it's marked with recurseForDerivations).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.