~aasg/nixexprs

f824a7653064193d669047a32fe4908c3302cedf — Aluísio Augusto Silva Gonçalves a month ago 8ad7463
lib: Drop Nix off declarative environment function

That really doesn't play nice when you want `nixUnstable` instead.
Now we put that in a `baseEnvironment` that can be overriden.

This is a breaking change.
2 files changed, 58 insertions(+), 36 deletions(-)

M lib/declarative-env.nix
M lib/default.nix
M lib/declarative-env.nix => lib/declarative-env.nix +56 -35
@@ 1,38 1,59 @@
{ lib, ... }:
let
  # Based on https://gist.github.com/lheckemann/402e61e8e53f136f239ecd8c17ab1deb
  declareEnvironmentRaw =
    { file # Path to the environment definition file
    , withPkgs # Function returning the packages to install
    , name ? lib.removeSuffix ".nix" (baseNameOf file) # Name of the environment
    , drvName ? "${name}-environment" # Name of the generated derivaqtion
    , profile ? "/nix/var/nix/profiles/${name}" # Path to the profile
    , pkgs ? import <nixpkgs> { } # Nixpkgs
    }:
    pkgs.buildEnv {
      name = drvName;
      extraOutputsToInstall = [ "out" "bin" "lib" ];
      paths = (withPkgs pkgs) ++ [
        # Script to rebuild the profile from the same input file.
        (pkgs.writeScriptBin "update-profile" ''
          #!${pkgs.stdenv.shell}
          nix-env -p ${profile} --set -f ${file} --argstr drvName "${name}-environment-$(date -I)"
        '')
        # Manifest to make sure imperative nix-env doesn't work
        # (otherwise it will overwrite the profile, removing all
        # packages other than the newly-installed one).
        (pkgs.writeTextFile {
          name = "break-nix-env-manifest";
          destination = "/manifest.nix";
          text = ''
            throw "Your user environment is a buildEnv which is incompatible with nix-env's built-in env builder. Edit your home expression and run update-profile instead!"
          '';
        })
        # Nixpkgs version, in case the user needs it.
        (pkgs.writeTextFile {
          name = "nixpkgs-version";
          destination = "/nixpkgs-version";
          text = pkgs.lib.version;
        })
      ];
    };
in
rec {
  /* declareEnvironment :: { ... } -> derivation
   *
   * Create a declarative user environment for package management.
   *
   * You probably want to override `baseEnvironment` instead of calling
   * this directly, as the former includes Nix in the environment.
   */
  declareEnvironment = lib.makeOverridable declareEnvironmentRaw;

# Based on https://gist.github.com/lheckemann/402e61e8e53f136f239ecd8c17ab1deb
{ file # Path to the environment definition file
, withPkgs # Function returning the packages to install
, name ? lib.removeSuffix ".nix" (baseNameOf file) # Name of the environment
, drvName ? "${name}-environment" # Name of the generated derivaqtion
, profile ? "/nix/var/nix/profiles/${name}" # Path to the profile
, pkgs ? import <nixpkgs> { } # Nixpkgs
}: with pkgs;

buildEnv {
  name = drvName;
  extraOutputsToInstall = [ "out" "bin" "lib" ];
  paths = (withPkgs pkgs) ++ [
    nix
    glibcLocales

    (writeScriptBin "update-profile" ''
      #!${stdenv.shell}
      nix-env -p ${profile} --set -f ${file} --argstr drvName "${name}-environment-$(date -I)"
    '')
    # Manifest to make sure imperative nix-env doesn't work (otherwise it will overwrite the profile, removing all packages other than the newly-installed one).
    (writeTextFile {
      name = "break-nix-env-manifest";
      destination = "/manifest.nix";
      text = ''
        throw "Your user environment is a buildEnv which is incompatible with nix-env's built-in env builder. Edit your home expression and run update-profile instead!"
      '';
    })
    # To allow easily seeing which nixpkgs version the profile was built from, place the version string in ~/.nix-profile/nixpkgs-version
    (writeTextFile {
      name = "nixpkgs-version";
      destination = "/nixpkgs-version";
      text = pkgs.lib.version;
    })
  ];
  /*
   * A minimally working declarative environment, with Nix and the
   * Glibc locales.  Split off declareEnvironment so that Nix can be
   * overriden with `nixUnstable`.
   */
  baseEnvironment = declareEnvironment {
    file = throw "Do not use baseEnvironment directly; override it instead.";
    withPkgs = pkgs: with pkgs; [ nix glibcLocales ];
  };
}

M lib/default.nix => lib/default.nix +2 -1
@@ 5,7 5,8 @@ let
    attrsets = callLibs ./attrsets.nix;
    inherit (attrsets) capitalizeAttrNames updateNew updateNewRecursive;

    declareEnvironment = lib.makeOverridable (callLibs ./declarative-env.nix);
    declarativeEnvironments = callLibs ./declarative-env.nix;
    inherit (declarativeEnvironments) declareEnvironment baseEnvironment;

    extended = import ./extension.nix { inherit lib; };