2023-07-15 17:15:38 +00:00
|
|
|
/*
|
|
|
|
Manages the things that are needed for a traditional nix-channel based
|
|
|
|
configuration to work.
|
|
|
|
|
|
|
|
See also
|
2023-08-04 22:07:22 +00:00
|
|
|
- ./nix.nix
|
|
|
|
- ./nix-flakes.nix
|
2023-07-15 17:15:38 +00:00
|
|
|
*/
|
|
|
|
{ config, lib, ... }:
|
|
|
|
let
|
|
|
|
inherit (lib)
|
2023-08-04 22:07:22 +00:00
|
|
|
mkDefault
|
2023-07-15 17:15:38 +00:00
|
|
|
mkIf
|
|
|
|
mkOption
|
|
|
|
stringAfter
|
|
|
|
types
|
|
|
|
;
|
|
|
|
|
|
|
|
cfg = config.nix;
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
nix = {
|
2023-08-04 22:07:22 +00:00
|
|
|
channel = {
|
|
|
|
enable = mkOption {
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether the `nix-channel` command and state files are made available on the machine.
|
|
|
|
|
|
|
|
The following files are initialized when enabled:
|
|
|
|
- `/nix/var/nix/profiles/per-user/root/channels`
|
|
|
|
- `/root/.nix-channels`
|
|
|
|
- `$HOME/.nix-defexpr/channels` (on login)
|
|
|
|
|
|
|
|
Disabling this option will not remove the state files from the system.
|
|
|
|
'';
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
nixPath = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2023-08-04 22:07:22 +00:00
|
|
|
default =
|
|
|
|
if cfg.channel.enable
|
|
|
|
then [
|
|
|
|
"nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
|
|
|
|
"nixos-config=/etc/nixos/configuration.nix"
|
|
|
|
"/nix/var/nix/profiles/per-user/root/channels"
|
|
|
|
]
|
|
|
|
else [ ];
|
|
|
|
defaultText = ''
|
|
|
|
if nix.channel.enable
|
|
|
|
then [
|
|
|
|
"nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
|
|
|
|
"nixos-config=/etc/nixos/configuration.nix"
|
|
|
|
"/nix/var/nix/profiles/per-user/root/channels"
|
|
|
|
]
|
|
|
|
else [];
|
|
|
|
'';
|
2023-07-15 17:15:38 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The default Nix expression search path, used by the Nix
|
|
|
|
evaluator to look up paths enclosed in angle brackets
|
|
|
|
(e.g. `<nixpkgs>`).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
system = {
|
|
|
|
defaultChannel = mkOption {
|
|
|
|
internal = true;
|
|
|
|
type = types.str;
|
|
|
|
default = "https://nixos.org/channels/nixos-unstable";
|
|
|
|
description = lib.mdDoc "Default NixOS channel to which the root user is subscribed.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
environment.extraInit =
|
2023-08-04 22:07:22 +00:00
|
|
|
mkIf cfg.channel.enable ''
|
2023-07-15 17:15:38 +00:00
|
|
|
if [ -e "$HOME/.nix-defexpr/channels" ]; then
|
|
|
|
export NIX_PATH="$HOME/.nix-defexpr/channels''${NIX_PATH:+:$NIX_PATH}"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
environment.extraSetup = mkIf (!cfg.channel.enable) ''
|
|
|
|
rm --force $out/bin/nix-channel
|
|
|
|
'';
|
|
|
|
|
|
|
|
# NIX_PATH has a non-empty default according to Nix docs, so we don't unset
|
|
|
|
# it when empty.
|
2023-07-15 17:15:38 +00:00
|
|
|
environment.sessionVariables = {
|
|
|
|
NIX_PATH = cfg.nixPath;
|
|
|
|
};
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
nix.settings.nix-path = mkIf (! cfg.channel.enable) (mkDefault "");
|
|
|
|
|
|
|
|
system.activationScripts.nix-channel = mkIf cfg.channel.enable
|
|
|
|
(stringAfter [ "etc" "users" ] ''
|
2023-07-15 17:15:38 +00:00
|
|
|
# Subscribe the root user to the NixOS channel by default.
|
|
|
|
if [ ! -e "/root/.nix-channels" ]; then
|
|
|
|
echo "${config.system.defaultChannel} nixos" > "/root/.nix-channels"
|
|
|
|
fi
|
2023-08-04 22:07:22 +00:00
|
|
|
'');
|
2023-07-15 17:15:38 +00:00
|
|
|
};
|
|
|
|
}
|