From 87b57d5011a37a8bb4f0cf56d14288241fdc69c2 Mon Sep 17 00:00:00 2001 From: yukkop Date: Fri, 1 May 2026 20:39:38 +0000 Subject: [PATCH] feat: `linux-devshell`: init --- package/default.nix | 3 + package/linux-devshell/default.nix | 48 ++++++++++++++ package/linux-devshell/linux-devshell.sh | 81 ++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 package/linux-devshell/default.nix create mode 100644 package/linux-devshell/linux-devshell.sh diff --git a/package/default.nix b/package/default.nix index 7652f1e..b289679 100644 --- a/package/default.nix +++ b/package/default.nix @@ -108,6 +108,7 @@ nativeBuildInputs = with pkgs; [pkg-config curl]; }; dbToolPkgs = pkgs.callPackage ./db-tool { inherit self; }; + linuxDevShellPkgs = pkgs.callPackage ./linux-devshell {}; in { py3-datetime = pkgs.callPackage ./py3-datetime.nix {}; py3-marzban = pkgs.callPackage ./py3-marzban.nix { inherit self; }; @@ -146,6 +147,8 @@ in { "postgres-init" = dbToolPkgs."postgres-init"; "postgres-cleanup" = dbToolPkgs."postgres-cleanup"; "hectic-inheritance" = dbToolPkgs."hectic-inheritance"; + "linux-devshell" = linuxDevShellPkgs.linux-devshell; + "linux-devshell-standalone" = linuxDevShellPkgs.linux-devshell-standalone; nbt2json = pkgs.callPackage ./nbt2json {}; hemar-parser = pkgs.callPackage ./hemar/parser {}; AstroTuxLauncher = pkgs.callPackage ./AstroTuxLauncher.nix {}; diff --git a/package/linux-devshell/default.nix b/package/linux-devshell/default.nix new file mode 100644 index 0000000..37f6d31 --- /dev/null +++ b/package/linux-devshell/default.nix @@ -0,0 +1,48 @@ +{ dash, hectic, curl, coreutils, gawk, procps, writeTextFile, lib }: +let + shell = "${dash}/bin/dash"; + bashOptions = [ + "errexit" + "nounset" + ]; + + logHelpers = builtins.readFile ../../lib/shell/logs.sh; + scriptText = builtins.readFile ./linux-devshell.sh; + + linuxDevShell = hectic.writeShellApplication { + inherit shell bashOptions; + name = "linux-devshell"; + runtimeInputs = [ curl coreutils gawk procps ]; + excludeShellChecks = [ "SC2034" "SC1090" ]; + + text = '' + ${logHelpers} + ${scriptText} + ''; + + meta = { + description = "Install Nix and enter development shell"; + mainProgram = "linux-devshell"; + }; + }; + + linuxDevShellStandalone = writeTextFile { + name = "linux-devshell"; + executable = true; + text = '' + #!${shell} + ${lib.concatMapStringsSep "\n" (option: "set -o ${option}") bashOptions} + + ${logHelpers} + ${scriptText} + ''; + meta = { + description = "Standalone linux-devshell script (single file)"; + mainProgram = "linux-devshell"; + }; + }; +in +{ + linux-devshell = linuxDevShell; + linux-devshell-standalone = linuxDevShellStandalone; +} diff --git a/package/linux-devshell/linux-devshell.sh b/package/linux-devshell/linux-devshell.sh new file mode 100644 index 0000000..224b133 --- /dev/null +++ b/package/linux-devshell/linux-devshell.sh @@ -0,0 +1,81 @@ +NIX_HOOK="${HOME}/.nix-profile/etc/profile.d/nix.sh" + +detect_shell() { + self=detect_shell + + if [ "$0" != "$self" ]; then + case "$0" in + /*) [ -x "$0" ] && { printf '%s\n' "$0"; return 0; } ;; + *) command -v "$0" 2>/dev/null && return 0 ;; + esac + fi + + _shell="$(ps -p $$ -o args= 2>/dev/null | awk '{print $1}')" + [ -n "$_shell" ] && command -v "$_shell" 2>/dev/null && { + command -v "$_shell" + return 0 + } + + [ -r "/proc/$$/exe" ] && readlink -f "/proc/$$/exe" && return 0 + + [ -x "$SHELL" ] && printf '%s\n' "$SHELL" && return 0 + + return 1 +} + +install_nix() { + log_info "Nix not found. Installing via nixos.org/nix/install --no-daemon..." + + if ! command -v curl >/dev/null 2>&1; then + log_error "curl is required. Install it with: sudo pacman -S curl" + exit 1 + fi + + NIX_INSTALL="$(mktemp)" + trap 'rm -f "$NIX_INSTALL"' EXIT + + if ! curl -sSfL https://nixos.org/nix/install -o "$NIX_INSTALL"; then + log_error "Failed to download Nix installer." + exit 1 + fi + + sh "$NIX_INSTALL" --no-daemon || true + + NIX_CONF_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/nix" + mkdir -p "$NIX_CONF_DIR" + NIX_CONF="${NIX_CONF_DIR}/nix.conf" + if ! grep -q 'build-users-group' "$NIX_CONF" 2>/dev/null; then + printf 'build-users-group =\nextra-experimental-features = nix-command flakes\n' >> "$NIX_CONF" + log_info "Patched nix.conf: disabled build-users-group." + fi + + log_info "Nix installed successfully." +} + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" + +if [ -f "$NIX_HOOK" ]; then + . "$NIX_HOOK" +fi + +if ! command -v nix >/dev/null 2>&1; then + install_nix + if [ -f "$NIX_HOOK" ]; then + . "$NIX_HOOK" + fi + if ! command -v nix >/dev/null 2>&1; then + log_error "Nix binary still not found after installation. Try opening a new terminal." + exit 1 + fi +else + log_info "Nix is already installed -- skipping installation." +fi + +CURR_SHELL="$(detect_shell)" +log_info "Entering dev shell (nix develop) with shell: ${CURR_SHELL}..." +log_info "Repository: ${REPO_ROOT}" + +exec nix --extra-experimental-features 'nix-command flakes' \ + develop "${REPO_ROOT}" \ + -c "$CURR_SHELL"