A new start

This commit is contained in:
Harald Hoyer 2024-03-21 15:00:36 +01:00
commit f4e2368893
93 changed files with 7621 additions and 0 deletions

View file

@ -0,0 +1,49 @@
{ lib
, writeText
, writeShellApplication
, substituteAll
, gum
, inputs
, hosts ? { }
, ...
}:
let
inherit (lib) mapAttrsToList concatStringsSep;
inherit (lib.metacfg) override-meta;
substitute = args: builtins.readFile (substituteAll args);
formatted-hosts = mapAttrsToList
(name: host: "${name},${host.pkgs.system}")
hosts;
hosts-csv = writeText "hosts.csv" ''
Name,System
${concatStringsSep "\n" formatted-hosts}
'';
nixos-hosts = writeShellApplication {
name = "nixos-hosts";
text = substitute {
src = ./nixos-hosts.sh;
help = ./help;
hosts = if hosts == { } then "" else hosts-csv;
};
checkPhase = "";
runtimeInputs = [
gum
];
};
new-meta = with lib; {
description = "A helper to list all of the NixOS hosts available from your flake.";
license = licenses.asl20;
maintainers = with maintainers; [ jakehamilton ];
};
in
override-meta new-meta nixos-hosts

View file

@ -0,0 +1,16 @@
echo -e "
${text_bold}${text_fg_blue}nixos-hosts${text_reset}
${text_bold}DESCRIPTION${text_reset}
Show NixOS hosts from your flake.
${text_bold}USAGE${text_reset}
${text_dim}\$${text_reset} ${text_bold}nixos-hosts${text_reset} [options]
${text_bold}OPTIONS${text_reset}
--help, -h Show this help message
--debug Show debug messages
"

View file

@ -0,0 +1,324 @@
#!/usr/bin/env bash
#==============================#
# Global #
#==============================#
DEBUG=${DEBUG:-"false"}
#==============================#
# Injected #
#==============================#
hosts="@hosts@"
help_root="@help@"
#==============================#
# Logging #
#==============================#
text_reset="\e[m"
text_bold="\e[1m"
text_dim="\e[2m"
text_italic="\e[3m"
text_underline="\e[4m"
text_blink="\e[5m"
text_highlight="\e[7m"
text_hidden="\e[8m"
text_strike="\e[9m"
text_fg_red="\e[38;5;1m"
text_fg_green="\e[38;5;2m"
text_fg_yellow="\e[38;5;3m"
text_fg_blue="\e[38;5;4m"
text_fg_magenta="\e[38;5;5m"
text_fg_cyan="\e[38;5;6m"
text_fg_white="\e[38;5;7m"
text_fg_dim="\e[38;5;8m"
text_bg_red="\e[48;5;1m"
text_bg_green="\e[48;5;2m"
text_bg_yellow="\e[48;5;3m"
text_bg_blue="\e[48;5;4m"
text_bg_magenta="\e[48;5;5m"
text_bg_cyan="\e[48;5;6m"
text_bg_white="\e[48;5;7m"
text_bg_dim="\e[48;5;8m"
# Usage: log_info <message>
log_info() {
echo -e "${text_fg_blue}info${text_reset} $1"
}
# Usage: log_todo <message>
log_todo() {
echo -e "${text_bg_magenta}${text_fg_white}todo${text_reset} $1"
}
# Usage: log_debug <message>
log_debug() {
if [[ $DEBUG == true ]]; then
echo -e "${text_fg_dim}debug${text_reset} $1"
fi
}
# Usage: log_warn <message>
log_warn() {
echo -e "${text_fg_yellow}warn${text_reset} $1"
}
# Usage: log_error <message>
log_error() {
echo -e "${text_fg_red}error${text_reset} $1"
}
# Usage: log_fatal <message> [exit-code]
log_fatal() {
echo -e "${text_fg_white}${text_bg_red}fatal${text_reset} $1"
if [ -z ${2:-} ]; then
exit 1
else
exit $2
fi
}
# Usage: clear_previous_line [number]
clear_line() {
echo -e "\e[${1:-"1"}A\e[2K"
}
# Usage:
# rewrite_line <message>
# rewrite_line <number> <message>
rewrite_line() {
if [[ $# == 1 ]]; then
echo -e "\e[1A\e[2K${1}"
else
echo -e "\e[${1}A\e[2K${2}"
fi
}
#==============================#
# Options #
#==============================#
positional_args=()
opt_help=false
opt_pick=false
opt_list=false
# Usage: missing_value <option>
missing_opt_value() {
log_fatal "Option $1 requires a value"
}
# shellcheck disable=SC2154
while [[ $# > 0 ]]; do
case "$1" in
-h|--help)
opt_help=true
shift
;;
-p|--pick)
opt_pick=true
shift
;;
-l|--list)
opt_list=true
shift
;;
--show-trace)
opt_show_trace=true
shift
;;
--debug)
DEBUG=true
shift
;;
--)
shift
break
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
positional_args+=("$1")
shift
;;
esac
done
passthrough_args=($@)
show_trace=""
#==============================#
# Helpers #
#==============================#
# Usage: split <string> <delimiter>
split() {
IFS=$'\n' read -d "" -ra arr <<< "${1//$2/$'\n'}"
printf '%s\n' "${arr[@]}"
}
# Usage: lstrip <string> <pattern>
lstrip() {
printf '%s\n' "${1##$2}"
}
# Usage: show_help <path>
show_help() {
log_debug "Showing help: ${text_bold}$1${text_reset}"
source "${help_root}/$1.sh"
}
# Usage: require_flake_nix
require_flake_nix() {
if ! [ -f ./flake.nix ]; then
log_fatal "This command requires a flake.nix file, but one was not found in the current directory."
fi
}
# Usage: get_flake_attributes <output> <flake-uri>
get_flake_attributes() {
local outputs=""
# NOTE: Some flakes may not contain the values we're looking for. In
# which case, we swallow errors here to keep the output clean.
set +e
outputs=$(nix eval --impure --raw --expr "\
let
flake = builtins.getFlake \"$2\";
outputs =
if builtins.elem \"$1\" [ \"packages\" \"devShells\" \"apps\" ] then
builtins.attrNames (flake.\"$1\".\${builtins.currentSystem} or {})
else
builtins.attrNames (flake.\"$1\" or {});
names = builtins.map (output: \"$2#\" + output) outputs;
in
builtins.concatStringsSep \" \" names
" 2>/dev/null)
set -e
echo "$outputs"
}
# Usage: replace_each <pattern> <text> <data>
replace_each() {
local results=()
for item in $3; do
if [[ "$item" == $1* ]]; then
results+=("$2$(lstrip $item $1)")
else
results+=("$item")
fi
done
echo "${results[*]}"
}
# Usage: prefix_each <prefix> <data>
prefix_each() {
local results=()
for item in $2; do
results+=("$1 $item")
done
echo "${results[*]}"
}
# Usage: join_by <delimiter> <data> <data> [...<data>]
join_by() {
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
# Usage: privileged <command>
privileged() {
cmd="$@"
if command -v sudo >/dev/null; then
log_debug "sudo $cmd"
sudo $cmd
elif command -v doas >/dev/null; then
log_debug "doas $cmd"
doas $cmd
else
log_warn "Could not find ${text_bold}sudo${text_reset} or ${text_bold}doas${text_reset}. Executing without privileges."
log_debug "$cmd"
eval "$cmd"
fi
}
#==============================#
# Commands #
#==============================#
nixos_hosts_all() {
if [[ $opt_help == true ]]; then
show_help nixos-hosts
exit 0
fi
if [[ -z "${hosts}" ]]; then
log_error "To use this program, override it with your nixosConfigurations:"
log_error ""
log_error " nixos-hosts.override {"
log_error " inherit (self) nixosConfigurations;"
log_error " }"
log_error ""
log_fatal "No hosts configured."
fi
if [ "${opt_list}" == "true" ]; then
local csv=$(cat "${hosts}")
local csv_data=$(echo -n "$csv" | tail +2)
while read -r line; do
local name=$(split "${line}" "," | head -n 1)
echo "${name}"
done <<< "${csv_data}"
exit 0
fi
host=$(gum table --selected.foreground="4" --widths=32,20 < "${hosts}")
if [ -z "${host}" ]; then
exit 0
else
local name=$(split "${host}" "," | head -n 1)
echo $name
fi
}
#==============================#
# Execute #
#==============================#
if [ -z "${positional_args:-}" ]; then
if [[ $opt_help == true ]]; then
show_help "nixos-hosts"
exit 0
else
log_debug "Running subcommand: ${text_bold}nixos_hosts_all${text_reset}"
nixos_hosts_all
exit 0
fi
fi
case ${positional_args[0]} in
*)
log_fatal "Unknown subcommand: ${text_bold}${positional_args[0]}${text_reset}"
;;
esac

View file

@ -0,0 +1,73 @@
{ pkgs
, lib
, gitHostCommitUrl ? "https://git.hoyer.xyz/harald/nixcfg/commit"
, ...
}:
let
inherit (lib.metacfg) override-meta;
new-meta = with lib; {
description = "A helper show the current git revision of the system configuration.";
license = licenses.asl20;
maintainers = with maintainers; [ jakehamilton ];
};
package =
pkgs.writeShellScriptBin "nixos-revision" ''
HAS_HELP=false
HAS_OPEN=false
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
HAS_HELP=true
shift
;;
-o|--open)
HAS_OPEN=true
shift
;;
*)
shift
;;
esac
done
if [ $HAS_HELP == true ]; then
HELP_MSG="
nixos-revision
USAGE
nixos-revision [options]
OPTIONS
-h, --help Show this help message
-o, --open Open the revision on GitHub
EXAMPLES
$ # Print the current revision
$ nixos-revision
$ # Open the current revision on GitHub
$ nixos-revision --open
"
echo "$HELP_MSG"
exit 0
fi
REVISION=$(nixos-version --json | ${pkgs.jq}/bin/jq -r .configurationRevision)
if [ $HAS_OPEN == true ]; then
GITHUB_URL="${gitHostCommitUrl}/$REVISION"
echo "Opening URL: $GITHUB_URL"
${pkgs.xdg-utils}/bin/xdg-open $GITHUB_URL
else
echo $REVISION
fi
'';
in
override-meta new-meta package

54
packages/rot8000/Cargo.lock generated Normal file
View file

@ -0,0 +1,54 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "memchr"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "regex"
version = "1.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rot8000"
version = "0.1.0"
dependencies = [
"regex",
]

View file

@ -0,0 +1,28 @@
{ lib
, stdenv
, rustPlatform
, fetchFromGitHub
, ...
}:
rustPlatform.buildRustPackage rec {
pname = "rot8000";
version = "0.0.1";
src = fetchFromGitHub {
owner = "haraldh";
rev = "3e3c4e4d6ed5b572ca2b09c8dc7f5bc3bd86e554";
repo = "mailsmudge";
sha256 = "sha256-4cG4Mn94lewxj1qstor8R0xLWIBFxf0rOHTMxzgHyFQ=";
};
cargoLock = {
lockFile = ./Cargo.lock;
};
meta = with lib; {
description = "mailsmudge";
homepage = "https://github.com/haraldh/mailsmudge";
license = licenses.unlicense;
maintainers = [ maintainers.tailhook ];
};
}