From 0d4b6f0107b45005eefcb937f16f9f0de171133b Mon Sep 17 00:00:00 2001 From: krizej Date: Wed, 16 Oct 2024 14:07:58 +0200 Subject: [PATCH] refactor fish.nix --- hosts/desktop/default.nix | 1 - modules/stuff/fish.nix | 65 +++++++++++++++++++++++---------------- modules/system.nix | 14 +++++++-- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/hosts/desktop/default.nix b/hosts/desktop/default.nix index 4678c29..ce2c976 100644 --- a/hosts/desktop/default.nix +++ b/hosts/desktop/default.nix @@ -38,7 +38,6 @@ pamixer sqlitebrowser obs-studio - yt-dlp aseprite cloc viu diff --git a/modules/stuff/fish.nix b/modules/stuff/fish.nix index cf35b9c..d451826 100644 --- a/modules/stuff/fish.nix +++ b/modules/stuff/fish.nix @@ -1,51 +1,64 @@ -{ lib, pkgs, config, hostname, ... }: let - inherit (lib) mkIf mkEnableOption mkOption types mergeAttrs; +{ lib, pkgs, config, ... }: let + inherit (lib) mkIf mkEnableOption mkOption types; cfg = config.chuj.stuff.fish; user = config.chuj.system.user; in { options.chuj.stuff.fish = { enable = mkEnableOption "fish"; - extraAliases = mkOption { + aliases = mkOption { + type = types.attrs; + default = {}; + }; + abbrs = mkOption { + type = types.attrs; + default = {}; + }; + functions = mkOption { type = types.attrs; default = {}; }; }; - config = let - XDG_DATA_HOME = config.home-manager.users.${user}.xdg.dataHome; - in mkIf cfg.enable { + config = mkIf cfg.enable { environment.systemPackages = [ pkgs.fish ]; programs.fish.enable = true; users.users.${user}.shell = pkgs.fish; programs.fish.promptInit = '' - ${pkgs.any-nix-shell}/bin/any-nix-shell fish --info-right | source + ${lib.getExe pkgs.any-nix-shell} fish --info-right | source ''; home-manager.users.${user}.programs.fish = { enable = true; - # todo: move some to abbrs - shellAliases = mergeAttrs { - "ls" = "${pkgs.eza}/bin/eza -lhs type"; + shellAliases = let + XDG_DATA_HOME = config.home-manager.users.${user}.xdg.dataHome; + yt-dlp = lib.getExe pkgs.yt-dlp; + curl = lib.getExe pkgs.curl; + wget = lib.getExe pkgs.wget; + xclip = lib.getExe pkgs.xclip; + in { + "ytmp3" = "${yt-dlp} -o '%(title)s.%(ext)s' -x --audio-format mp3"; + "myip" = "${curl} ifconfig.me"; + "wget" = "${wget} --hsts-file='${XDG_DATA_HOME}/wget-hsts'"; + "2clip" = "${xclip} -selection clipboard"; + } // cfg.aliases; + + shellAbbrs = { + "ls" = "eza -lhs type"; "cp" = "cp -v"; "mv" = "mv -v"; - "ytmp3" = "yt-dlp -o '%(title)s.%(ext)s' -x --audio-format mp3"; - "myip" = "curl ifconfig.me"; - "0x0" = ''curl -F"file=@$argv" https://0x0.st''; - "wget" = "${pkgs.wget}/bin/wget --hsts-file='${XDG_DATA_HOME}/wget-hsts'"; + } // cfg.abbrs; - # ulimit -n 4096 fixes the "too many open files" error i get sometimes when - # updating the entire flake - "rebuild" = "ulimit -n 4096 && sudo nixos-rebuild switch --flake ~/nix#${hostname} -v"; - } cfg.extraAliases; - - shellAbbrs = { # todo: mkif xclip in pkgs - "2clip" = "xclip -selection clipboard"; - }; - - functions = { - "fish_greeting" = "${(pkgs.fortune.override { withOffensive = true; })}/bin/fortune"; + functions = let + readlink = lib.getExe' pkgs.coreutils "readlink"; + which = lib.getExe pkgs.which; + curl = lib.getExe pkgs.curl; + fortune = lib.getExe (pkgs.fortune.override { withOffensive = true; }); + in { + "WHICH" = "${readlink} -f $(${which} $argv[1])"; + "0x0" = ''${curl} -F"file=@$argv[1]" https://0x0.st''; + "fish_greeting" = "${fortune}"; "fish_prompt" = '' # name: Informative Vcs # author: Mariusz Smykula @@ -112,7 +125,7 @@ in { echo -n "$suffix " # end ''; - }; + } // cfg.functions; }; }; } diff --git a/modules/system.nix b/modules/system.nix index 4541ee2..07a0f6e 100644 --- a/modules/system.nix +++ b/modules/system.nix @@ -1,4 +1,4 @@ -{ lib, pkgs, config, ... } @ args: let +{ lib, pkgs, config, hostname, ... } @ args: let inherit (lib) mkOption types; cfg = config.chuj.system; in { @@ -57,7 +57,10 @@ in { ''; }; - environment.systemPackages = with pkgs; [ + environment.systemPackages = with pkgs; let + nixos-rebuild = lib.getExe pkgs.nixos-rebuild; + sudo = "/run/wrappers/bin/sudo"; # is this correct + in [ ed # is the standard text editor. ripgrep @@ -65,6 +68,7 @@ in { tree btop wget + eza zip unzip @@ -76,6 +80,12 @@ in { gcc gnumake python311 + + (writeShellScriptBin "rebuild" '' + set -e + ulimit -n 4096 # this fixes the "too many open files" error + ${sudo} ${nixos-rebuild} switch --flake ~/nix#${hostname} -v + '') ]; }; }