Skip to content

Instantly share code, notes, and snippets.

@573
Last active January 4, 2024 09:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 573/c06ccea910abcc8f9255cb8f3ace3e86 to your computer and use it in GitHub Desktop.
Save 573/c06ccea910abcc8f9255cb8f3ace3e86 to your computer and use it in GitHub Desktop.
nix repl for home-manager home.nix config

https://logs.nix.samueldr.com/home-manager/2020-06-24#3652401;

NIX_PATH=nixpkgs=http://nixos.org/channels/nixos-21.05/nixexprs.tar.xz:home-manager=https://github.com/nix-community/home-manager/archive/release-21.05.tar.gz:nix-on-droid=https://github.com/t184256/nix-on-droid/archive/release-21.05.tar.gz nix repl
nix-repl> hm = import <home-manager/modules> { configuration = ~/.config/nixpkgs/home.nix; pkgs = import <nixpkgs> {}; }
~> hm.config.TAB
@sebnyberg
Copy link

sebnyberg commented Jan 3, 2024

Very useful. Thanks for sharing.

Just to explain what is going on here, the $NIX_PATH makes <nixpkgs> and <home-manager> available in the repl. This can also be done with -I as per the nix repl documentation:

If a path in the Nix search path starts with http:// or https://, it is interpreted as the URL of a tarball that will be downloaded and unpacked to a temporary location. The tarball must consist of a single top-level directory. For example, passing

-I nixpkgs=https://github.com/NixOS/nixpkgs/archive/master.tar.gz

to nix repl tells Nix to download and use the current contents of the master branch in the nixpkgs repository.

I'm using nix flakes, so I wanted a way to load the flake (with :lf .) and use its inputs to generate the config within nix repl. In my directory I have flake.nix and home.nix. Not sure if it's idiomatic, but this works:

nix repl
nix-repl> :lf .
nix-repl> system = "x86_64-darwin"
nix-repl> pkgs = inputs.nixpkgs.legacyPackages.${system}
nix-repl> hm = inputs.home-manager.lib.homeManagerConfiguration { inherit pkgs; modules = [ ./home.nix ]; }
nix-repl> hm.config.<TAB>

@573
Copy link
Author

573 commented Jan 4, 2024

Thanks, glad you like it.

The flakes way you described I'd go for nowadays as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment