Skip to content

Commit

Permalink
Package homemade shell scripts with writeShellApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Mar 11, 2024
1 parent c766ffc commit 0a9338d
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 105 deletions.
226 changes: 124 additions & 102 deletions home-manager/homemade.nix
Original file line number Diff line number Diff line change
@@ -1,116 +1,138 @@
{ pkgs, lib, ... }:
{ pkgs, ... }:

# - Tiny tools by me, they may be rewritten with another language.
# - Aliases across multiple shells
let
la = pkgs.writeShellScript "la" ''
set -euo pipefail
${lib.getBin pkgs.eza}/bin/eza --long --all --group-directories-first --color=always "$@"
'';
la = pkgs.writeShellApplication {
name = "la";
runtimeInputs = with pkgs; [ eza ];
text = ''
eza --long --all --group-directories-first --color=always "$@"
'';
};
in
[
(pkgs.writeShellScript "bench_shells" ''
set -euo pipefail
# ~ my feeling ~
# 50ms : blazing fast!
# 110ms : acceptable
# 150ms : slow
# 200ms : 1980s?
# 300ms : much slow!
# zsh should be first, because it often makes much slower with the completion
${lib.getBin pkgs.hyperfine}/bin/hyperfine --warmup 1 --runs 5 \
'${lib.getExe pkgs.zsh} --interactive -c exit' \
'${lib.getExe pkgs.bashInteractive} -i -c exit' \
'${lib.getExe pkgs.fish} --interactive --command exit'
'')

(pkgs.writeShellScript
"updeps"
''
set -euxo pipefail
echo 'this updater assume you have the privilege and sudo command'
case ''${OSTYPE} in
linux*)
sudo apt update --yes && sudo apt upgrade --yes
;;
darwin*)
softwareupdate --install --recommended
;;
esac
sudo -i nix upgrade-nix
${lib.getExe pkgs.mise} plugins update
'')
(pkgs.writeShellApplication
{
name = "bench_shells";
runtimeInputs = with pkgs; [ hyperfine zsh bashInteractive fish ];
text = ''
# ~ my feeling ~
# 50ms : blazing fast!
# 110ms : acceptable
# 150ms : slow
# 200ms : 1980s?
# 300ms : much slow!
# zsh should be first, because it often makes much slower with the completion
hyperfine --warmup 1 --runs 5 \
'zsh --interactive -c exit' \
'bash -i -c exit' \
'fish --interactive --command exit'
'';
}
)

(pkgs.writeShellApplication
{
name = "updeps";
runtimeInputs = with pkgs; [ mise ];
text = ''
set -euxo pipefail
echo 'this updater assume you have the privilege and sudo command'
case ''${OSTYPE} in
linux*)
sudo apt update --yes && sudo apt upgrade --yes
;;
darwin*)
softwareupdate --install --recommended
;;
esac
sudo -i nix upgrade-nix
mise plugins update
'';
}
)

la

(pkgs.writeShellScript
"lat"
''
set -euo pipefail
${la} --tree "$@"
'')

(pkgs.writeShellScript
"walk"
''
set -euo pipefail
# TODO: Add --preview after nixpkgs include https://github.com/antonmedv/walk/pull/129
${lib.getBin pkgs.walk}/bin/walk --icons "$@"
'')
(pkgs.writeShellApplication
{
name = "lat";
runtimeInputs = [ la ];
text = ''
la --tree "$@"
'';
}
)

(pkgs.writeShellApplication
{
name = "wa";
runtimeInputs = with pkgs; [ walk ];
text = ''
# TODO: Add --preview after nixpkgs include https://github.com/antonmedv/walk/pull/129
walk --icons "$@"
'';
}
)

# Why need the wrapper?
# nixpkgs provide 4.9.3 is not including podman-remote.
# https://github.com/NixOS/nixpkgs/blob/e3474e1d1e53b70e2b2af73ea26d6340e82f6b8b/pkgs/applications/virtualization/podman/default.nix#L104-L108
(pkgs.writeShellScript
"podman"
''
set -euo pipefail
${lib.getBin pkgs.mise}/bin/mise exec podman@latest -- podman "$@"
'')

(pkgs.writeShellScript
"zj"
''
set -euo pipefail
name="$(${lib.getBin pkgs.coreutils}/bin/basename "$PWD")"
${lib.getBin pkgs.zellij}/bin/zellij attach "$name" || ${lib.getBin pkgs.zellij}/bin/zellij --session "$name"
'')

(pkgs.writeShellScript
"p"
''
set -euo pipefail
# Needless to trim the default command, nix-shell only runs last command if given multiple.
nix-shell --command "$SHELL" --packages "$@"
'')

(pkgs.writeShellScript
"git-delete-merged-branches"
''
set -euo pipefail
# Care these traps if you change this code
# - Prefer git built-in features to filter as possible, handling correct regex is often hard for human
# - grep returns false if empty, it does not fit for pipefail use. --no-run-if-empty as xargs does not exists in the grep options
# - Always specify --sort to ensure it can be used in comm command. AFAIK, refname is most fit key here.
# Make sure, this result should not be changed even if you changed in global git config with git.nix
# Candidates: https://github.com/git/git/blob/3c2a3fdc388747b9eaf4a4a4f2035c1c9ddb26d0/ref-filter.c#L902-L959
${lib.getBin pkgs.git}/bin/git branch --sort=refname --list main master trunk develop development |
${lib.getBin pkgs.coreutils}/bin/comm --check-order -13 - <(${lib.getBin pkgs.git}/bin/git branch --sort=refname --merged) |
${lib.getBin pkgs.findutils}/bin/xargs --no-run-if-empty --max-lines=1 ${lib.getBin pkgs.git}/bin/git branch --delete
'')
(pkgs.writeShellApplication
{
name = "podman";
runtimeInputs = with pkgs; [ mise ];
text = ''
mise exec podman@latest -- podman "$@"
'';
}
)


(pkgs.writeShellApplication
{
name = "zj";
runtimeInputs = with pkgs; [ coreutils zellij ];
text = ''
name="$(basename "$PWD")"
zellij attach "$name" || zellij --session "$name"
'';
}
)

(pkgs.writeShellApplication
{
name = "p";
runtimeInputs = with pkgs; [ nix ];
text = ''
# Needless to trim the default command, nix-shell only runs last command if given multiple.
nix-shell --command "$SHELL" --packages "$@"
'';
}
)

(pkgs.writeShellApplication
{
name = "git-delete-merged-branches";
runtimeInputs = with pkgs; [ git coreutils findutils ];
text = ''
# Care these traps if you change this code
# - Prefer git built-in features to filter as possible, handling correct regex is often hard for human
# - grep returns false if empty, it does not fit for pipefail use. --no-run-if-empty as xargs does not exists in the grep options
# - Always specify --sort to ensure it can be used in comm command. AFAIK, refname is most fit key here.
# Make sure, this result should not be changed even if you changed in global git config with git.nix
# Candidates: https://github.com/git/git/blob/3c2a3fdc388747b9eaf4a4a4f2035c1c9ddb26d0/ref-filter.c#L902-L959
git branch --sort=refname --list main master trunk develop development |
comm --check-order -13 - <(git branch --sort=refname --merged) |
xargs --no-run-if-empty --max-lines=1 git branch --delete
'';
}
)
]
5 changes: 2 additions & 3 deletions home-manager/packages.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{ pkgs, ... }:
{ pkgs, lib, ... }:

let homemades = import ./homemade.nix; in
{
home.packages = with pkgs; [
# Use `bashInteractive`, don't `bash` - https://github.com/NixOS/nixpkgs/issues/29960, https://github.com/NixOS/nix/issues/730
Expand Down Expand Up @@ -107,7 +106,7 @@ let homemades = import ./homemade.nix; in
# libyaml
# openssl

] ++ homemades ++ (lib.optionals stdenv.isLinux
] ++ (import ./homemade.nix { inherit pkgs; }) ++ (lib.optionals stdenv.isLinux
[
# Fix missing locales as `locale: Cannot set LC_CTYPE to default locale`
glibc
Expand Down

0 comments on commit 0a9338d

Please sign in to comment.