Title: Make nix flakes commands using the same nixpkgs as NixOS does
       Author: Solène
       Date: 20 July 2022
       Tags: nixos linux nix
       Description: This explains how to make the command nix shell
       nixpkgs#foobar to reuse nixpkgs from your NixOS system to save time and
       bandwidth
       
       # Introduction
       
       This article will explain how to make the flakes enabled nix commands
       reusing the nixpkgs repository used as input to build your NixOS
       system.  This will regularly save you time and bandwidth.
       
       # Flakes and registries
       
       By default, nix commands using flakes such as nix shell or nix run are
       pulling a tarball of the development version of nixpkgs.  This is the
       default value set in the nix registry for nixpkgs.
       
       ```shell
       $ nix registry list | grep nixpkgs
       global flake:nixpkgs github:NixOS/nixpkgs/nixpkgs-unstable
       ```
       
       Because of this, when you run a command, you are likely to download a
       tarball of the nixpkgs repository including the latest commit every
       time you use flakes, this is particularly annoying because the tarball
       is currently around 30 MB.  There is a simple way to automatically set
       your registry to define the nixpkgs repository to the local archive
       used by your NixOS configuration.
       
       To your `flake.nix` file describing your system configuration, you
       should have something similar to this:
       
       ```nix
       inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
       
       [...]
       nixosConfiguration = {
         my-computer =lib.nixosSystem {
           specialArgs = { inherit inputs; };
           [...]
         };
       };
       ```
       
       Edit /etc/nixos/configuration.nix and make sure you have "inputs"
       listed in the first line, such as:
       
       ```nix
       { lib, config, pkgs, inputs, ... }:
       ```
       
       
       ```nix
       nix.registry.nixpkgs.flake = inputs.nixpkgs;
       ```
       
       After this change, running a command such as "nix shell
       nixpkgs#gnumake" will reuse the same nixpkgs from your nix store used
       by NixOS, otherwise it would have been fetching the latest archive from
       GitHub.
       
       # nix-shell vs nix shell
       
       If you started using flakes, you may wonder why there are commands
       named "nix-shell" and "nix shell", they work totally differently.
       
       nix-shell and non flakes commands use the nixpkgs offered in the
       NIX_PATH environment variable, which should be set to a directory
       managed by nix-channel, but the channels are obsoleted by flakes...
       
       Fortunately, in the same way we synchronized the system flakes with the
       commands flakes, you can add this code to use the system nixpkgs with
       your nix-shell:
       
       ```nix
       nix.nixPath = [ "nixpkgs=/etc/channels/nixpkgs" "nixos-config=/etc/nixos/configuration.nix" "/nix/var/nix/profiles/per-user/root/channels" ];
       environment.etc."channels/nixpkgs".source = inputs.nixpkgs.outPath;
       ```
       
       This requires your user to logout from your current session to be
       effective.  You can then check nix-shell and nix shell use the same
       nixpkgs source with this snippet.  This asks the full path of the test
       program named "hello" and compares both results, they should match if
       they use the same nixpkgs.
       
       ```shell
       [ "$(nix-shell -p hello --run "which hello")" = "$(nix shell nixpkgs#hello -c which hello)" ] && echo success
       ```
       
       # Conclusion
       
       Flakes are awesome, and are in the way of becoming the future of Nix. I
       hope this article shed some light about nix commands, and saved you
       some bandwidth.
       
       # Credits
       
       I found this information on a blog post of the company Tweag (which is
       my current employer) in a series of articles about Nix flakes.  That's
       a bit sad I didn't find this information in the official NixOS
       documentation, but as flakes are still experimental, they are not
       really covered.
       
 (HTM) Tweag blog: Nix Flakes, Part 3: Managing NixOS systems
       
       As I found this information on their blog post, and I'm fine giving
       credits to people, so I have to link their blog post license here.
       
 (HTM) Creative Commons Attribution 4.0 International license