nixfiles/home-manager/modules/common.nix

318 lines
9.2 KiB
Nix
Raw Permalink Normal View History

{ lib, pkgsFlake, pkgs, pkgs', inputs, config, ... }@args:
2022-02-13 23:06:31 +00:00
let
2022-02-21 00:54:10 +00:00
inherit (builtins) listToAttrs mapAttrs readFile;
inherit (lib)
optionalString nameValuePair concatMapStrings concatStringsSep optionalAttrs versionAtLeast
2022-02-21 13:38:22 +00:00
mapAttrsToList mkMerge mkIf mkDefault mkOption;
2022-02-17 18:36:39 +00:00
inherit (lib.hm) dag;
inherit (lib.my) mkOpt' dummyOption;
2022-02-13 23:06:31 +00:00
in
{
options = with lib.types; {
my = {
isStandalone = mkOption {
type = bool;
internal = true;
description = "Whether home-manager is running inside a NixOS system or not.";
};
2022-02-17 18:36:39 +00:00
shell = mkOpt' str null "User's shell (so NixOS or others can set it externally).";
fishCompletionsFrequency = mkOpt' (nullOr str) "daily" "How often to generate fish completions from manpages.";
2022-02-17 18:36:39 +00:00
ssh = {
authKeys = {
literal = mkOpt' (listOf singleLineStr) [ ] "List of OpenSSH keys to allow";
2022-02-22 00:59:57 +00:00
files = mkOpt' (listOf path) [ ] "List of OpenSSH key files to allow";
2022-02-17 18:36:39 +00:00
};
};
};
};
config = mkMerge [
{
my = {
isStandalone = !(args ? osConfig);
shell = mkDefault "${config.programs.fish.package}/bin/fish";
};
home = {
file.".ssh/authorized_keys" = with config.my.ssh.authKeys;
mkIf (config.programs.ssh.enable && (literal != [ ] || files != [ ])) {
text = ''
${concatStringsSep "\n" literal}
${concatMapStrings (f: readFile f + "\n") files}
'';
};
};
nix = {
2022-11-20 04:44:22 +00:00
package = mkIf (!(versionAtLeast config.home.stateVersion "22.11")) pkgs.nix;
settings = with lib.my.c.nix; {
experimental-features = [ "nix-command" "flakes" "ca-derivations" ];
max-jobs = mkDefault "auto";
extra-substituters = cache.substituters;
extra-trusted-public-keys = cache.keys;
connect-timeout = 5;
fallback = true;
};
2022-02-13 23:06:31 +00:00
};
2022-02-14 00:56:43 +00:00
programs = {
# Even when enabled this will only be actually installed in standalone mode
# Note: `home-manager.path` is for telling home-manager is installed and setting it in NIX_PATH, which we should
# never care about.
home-manager.enable = true;
2022-02-17 15:47:24 +00:00
lsd = {
enable = mkDefault true;
enableAliases = mkDefault true;
};
starship = {
enable = mkDefault true;
settings = {
aws.disabled = true;
};
2022-02-21 00:54:10 +00:00
}
# We use custom behaviour for this
// listToAttrs (map (s: nameValuePair "enable${s}Integration" false) [ "Bash" "Zsh" "Fish" ]);
2022-02-17 15:47:24 +00:00
2022-03-23 13:39:13 +00:00
tmux = {
enable = true;
keyMode = "vi";
2022-03-23 13:39:13 +00:00
};
bash = {
# This does not install bash but has home-manager control .bashrc and friends
# Bash has some really weird behaviour with non-login and non-interactive shells, particularly around which
# of profile and bashrc are loaded when. This causes issues with PATH not being set correctly for
# non-interactive SSH...
enable = mkDefault true;
initExtra =
''
flake-src() {
cd "$(nix eval "''${@:2}" --impure --raw --expr "builtins.getFlake \"$1\"")"
}
'';
shellAliases = {
hm = "home-manager";
};
};
fish = {
enable = mkDefault true;
2022-02-21 00:54:10 +00:00
interactiveShellInit =
# TODO: Pull request?
(optionalString config.programs.starship.enable
''
# Adapted from https://github.com/nix-community/home-manager/blob/0232fe1b75e6d7864fd82b5c72f6646f87838fc3/modules/programs/starship.nix#L113
# linux is the VTTY, which doesn't seem to have a suitable font for starship
if test "$TERM" != "dumb" -a "$TERM" != "linux" -a \( -z "$INSIDE_EMACS" -o "$INSIDE_EMACS" = "vterm" \)
eval (${config.home.profileDirectory}/bin/starship init fish)
end
'');
functions = {
# Silence the default greeting
fish_greeting = ":";
flake-src = {
description = "cd into a flake reference's source directory";
body = ''cd (nix eval $argv[2..] --impure --raw --expr "builtins.getFlake \"$argv[1]\"")'';
};
};
shellAbbrs = {
hm = "home-manager";
k = "kubectl";
};
2023-05-06 19:44:24 +01:00
shellAliases = {
ip = "ip --color=auto";
s = "kitty +kitten ssh";
2023-05-06 19:44:24 +01:00
};
};
2022-02-17 18:36:39 +00:00
ssh = {
enable = mkDefault true;
matchBlocks = {
nix-dev-vm = {
user = "dev";
hostname = "localhost";
port = 2222;
extraOptions = {
StrictHostKeyChecking = "no";
UserKnownHostsFile = "/dev/null";
};
};
"rsync.net" = {
host = "rsyncnet";
user = "16413";
hostname = "ch-s010.rsync";
};
shoe = {
host = "shoe.netsoc.tcd.ie shoe";
user = "netsoc";
};
netsocBoxes = {
host = "cube spoon napalm gandalf saruman";
user = "root";
2022-02-17 18:36:39 +00:00
};
};
extraConfig =
''
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/netsoc
IdentityFile ~/.ssh/borg
'';
2022-02-17 18:36:39 +00:00
};
direnv = {
enable = mkDefault true;
nix-direnv.enable = true;
stdlib =
''
# addition to nix-direnv's use_nix that registers outputs as gc roots (as well as the .drv)
use_nix_outputs() {
local layout_dir drv deps
layout_dir="$(direnv_layout_dir)"
drv="$layout_dir/drv"
deps="$layout_dir/deps"
2022-02-13 23:06:31 +00:00
if [ ! -e "$deps" ] || (( "$(stat --format=%Z "$drv")" > "$(stat --format=%Z "$deps")" )); then
rm -rf "$deps"
mkdir -p "$deps"
nix-store --indirect --add-root "$deps/out" --realise $(nix-store --query --references "$drv") > /dev/null
log_status renewed outputs gc roots
fi
}
'';
};
2022-02-13 23:06:31 +00:00
htop = {
enable = true;
settings = {};
};
2022-02-15 01:08:00 +00:00
};
home = {
packages = with pkgs; [
file
tree
2023-06-24 17:31:26 +01:00
pwgen
minicom
iperf3
mosh
2022-03-23 13:39:13 +00:00
wget
2022-05-12 21:11:03 +01:00
whois
ldns
mtr
hyx
2022-06-06 15:09:42 +01:00
ncdu
2022-06-11 20:27:59 +01:00
jq
yq-go
nix-tree
];
sessionVariables = {
EDITOR = "vim";
};
2022-02-13 23:06:31 +00:00
language.base = mkDefault "en_IE.UTF-8";
};
}
2022-02-17 15:47:24 +00:00
(mkIf (config.my.isStandalone || !args.osConfig.home-manager.useGlobalPkgs) {
# Note: If globalPkgs mode is on, then these will be overridden by the NixOS equivalents of these options
nixpkgs = {
overlays = [
inputs.deploy-rs.overlay
2023-04-23 23:44:55 +01:00
inputs.boardie.overlays.default
2023-08-22 12:12:29 +01:00
inputs.nixGL.overlays.default
];
config = {
allowUnfree = true;
};
};
nix = {
registry = {
pkgs = {
to = {
type = "path";
path = "${pkgsFlake}";
};
exact = true;
};
};
};
})
(mkIf config.my.isStandalone {
2022-02-19 22:55:53 +00:00
my = {
2023-11-02 13:41:50 +00:00
ssh.authKeys.files = [ lib.my.c.sshKeyFiles.me ];
2022-02-19 22:55:53 +00:00
};
2022-11-20 04:44:22 +00:00
nix.package = mkIf (versionAtLeast config.home.stateVersion "22.05") pkgs.nix;
fonts.fontconfig.enable = true;
2022-02-15 01:08:00 +00:00
home = {
packages = with pkgs; [
pkgs'.mine.nix
];
# Without this, we are at the mercy of whatever version of nix is in $PATH...
# TODO: Is this the right thing to do?
extraActivationPath = [
config.nix.package
];
};
})
(mkIf pkgs.stdenv.isLinux (mkMerge [
{
home = {
packages = with pkgs; [
iputils
traceroute
];
};
}
(mkIf (config.my.isStandalone && config.programs.fish.enable && config.my.fishCompletionsFrequency != null) {
systemd.user = {
services.fish-update-completions = {
Unit.Description = "fish completions update";
Service = {
Type = "oneshot";
ExecStart = "${config.programs.fish.package}/bin/fish -c fish_update_completions";
};
};
timers.fish-update-completions = {
Unit.Description = "fish completions update timer";
Timer = {
OnCalendar = config.my.fishCompletionsFrequency;
Persistent = true;
Unit = "fish-update-completions.service";
};
Install.WantedBy = [ "timers.target" ];
};
};
})
]))
(mkIf (pkgs.stdenv.isDarwin && config.my.isStandalone) {
home = {
2022-02-21 13:58:38 +00:00
# No targets.genericLinux equivalent apparently
sessionVariablesExtra =
''
. "${config.nix.package}/etc/profile.d/nix.sh"
2022-02-21 13:58:38 +00:00
'';
packages = with pkgs; [
cacert
];
};
})
];
}