Single file home-manager kit

In the spirit of getting innocent and unsuspecting souls hooked on portable declarative packgage management, I’m wondering whether it’s possible create a not-too-ugly, self-contained and self-installing home-manager configuration, in a single file.

The idea is to make the process so simple, that anyone who knows which end of a keyboard to sit at, should be able to follow it. The proverbial grandmother, if you like.

Put another way, I should be able to say to someone, “Save this single file, run this single command, and you will have installed the N packages you want/need/we talked about over coffee”.

Assumptions / prerequisites

  1. nix-shell -p cowsay --run 'cowsay Nix is installed!' works in the victim’s account on the victim’s machine.

Rules of the Game

  1. Provide the victim with a SINGLE FILE. (Let’s assume, in what follows, that the victim saves it as $HOME/the-magic-nix-file, but any reasonable name will do. Maybe even .config/nixpkgs/home.nix, though a more obviously/sensibly version-controllable location would be preferable.)
  2. Ask the victim to exectue a SINGLE SHELL COMMAND, that uses $HOME/the-magic-nix-file.

Having completed these two steps, the victim should now have whatever packages were specified in home.packages inside the file.

  1. Changing the set of installed packages should consist of exactly 2 steps:

    a. updating home.packages in $HOME/the-magic-nix-file
    b.

    • EITHER repeat exactly the same command as was used in part 2
    • OR home-manager switch
  2. The pkgs version should be pinned.

Can you propose a solution?

Why?

I’ve have bootstrapped people with a single directory containing 3 files

  • bootstrap-home-manager
  • sources.nix
  • nixpkgs/home.nix

If I could reduce this to a single file, I would be able to persuade more people to give it a go, eventually leading them to benefit from the wonders that home-manager has to offer.

I would like the solution to be home-manager based, because there is an easy upgrade path to more involved configurations, for those victims who get hooked.

Alternatively, I would like to be able to answer the question

with something like

4 Likes

Hi.

Introducing tiny-home-manager! The full instructions and GitHub template are available at the link, but here is the complete file (shell.nix) and the command to run is nix-shell. The default configuration is to install bash as your shell and the program cowsay:

let

  nixpkgs-src = builtins.fetchTarball { url = "https://github.com/nixos/nixpkgs/archive/master.tar.gz"; };
  pkgs = import nixpkgs-src {};

  home-manager-src = builtins.fetchTarball { url = "https://github.com/rycee/home-manager/archive/master.tar.gz"; };
  home-manager = (import home-manager-src {inherit pkgs;}).home-manager;

  home.nix = ''
    { pkgs, ... }:
    {
      home.username = "$USER";
      home.homeDirectory = "$HOME";
      home.stateVersion = "20.09";
      programs.bash = {
        enable = true;
      };
      home.packages = [
        pkgs.cowsay
      ];
    }
  '';

in pkgs.mkShell rec {

  shellHook = ''
    export NIX_PATH="nixpkgs=${nixpkgs-src}:home-manager=${home-manager-src}"
    trap 'rm -f "$TMPFILE"' EXIT
    TMPFILE=$(mktemp) || exit 1
    export HOME_MANAGER_CONFIG="$TMPFILE"
    cat <<HOMENIX >$TMPFILE
    ${home.nix}
    HOMENIX
    ${home-manager}/bin/home-manager switch
    exit 0
  '';

}

I’d already worked out most of the details here for home-manager-template.

This doesn’t satisfy #4 but it should be relatively trivial for your hypothetical person setting this up to do.

Sincerely,
Ryan Mulligan

5 Likes

Obligatory mention, a tool by another nix genius- Static Nix: a command-line swiss army knife

3 Likes