From 1b528a15d07c350c7ebd52cc803f51c40f8bdbad Mon Sep 17 00:00:00 2001 From: matt1432 Date: Thu, 21 Nov 2024 12:59:47 -0500 Subject: [PATCH] feat(repl): make it work with onDroid --- packages/repl/default.nix | 51 ++++++++++++++++++++++------------ packages/repl/repl.nix | 58 ++++++++++++++------------------------- 2 files changed, 53 insertions(+), 56 deletions(-) diff --git a/packages/repl/default.nix b/packages/repl/default.nix index 1110e9c9..95ebb7e3 100644 --- a/packages/repl/default.nix +++ b/packages/repl/default.nix @@ -2,25 +2,40 @@ { coreutils, gnused, - writeShellScriptBin, + ncurses, + writeShellApplication, ... }: let repl = ./repl.nix; - example = command: desc: ''\n\u001b[33m ${command}\u001b[0m - ${desc}''; + example = command: desc: ''\n$(tput setaf 3) ${command}$(tput sgr0) - ${desc}''; in - writeShellScriptBin "repl" '' - case "$1" in - "-h"|"--help"|"help") - printf "%b\n\e[4mUsage\e[0m: \ - ${example "repl" "Loads system flake if available."} \ - ${example "repl /path/to/flake.nix" "Loads specified flake."}\n" - ;; - *) - if [ -z "$1" ]; then - nix repl --arg flakePath $(${coreutils}/bin/readlink -f "$FLAKE") --file ${repl} - else - nix repl --arg flakePath $(${coreutils}/bin/readlink -f $1 | ${gnused}/bin/sed 's|/flake.nix||') --file ${repl} - fi - ;; - esac - '' + writeShellApplication { + name = "repl"; + + runtimeInputs = [coreutils gnused ncurses]; + + text = '' + arg=''${1:-"."} + + case "$arg" in + "-h"|"--help"|"help") + # shellcheck disable=SC2059 + printf "\n\e[4mUsage\e[0m: \ + ${example "repl " "Loads system flake present at $(tput setaf 4)\\$FLAKE$(tput sgr0)."} \ + ${example "repl " "Loads specified flake."}\n" + ;; + + *) + if [ -z "$arg" ]; then + nix repl \ + --arg flakePath "$(realpath "$FLAKE")" \ + --file ${repl} + else + nix repl \ + --arg flakePath "$(realpath "$arg" | sed 's|/flake.nix||')" \ + --file ${repl} + fi + ;; + esac + ''; + } diff --git a/packages/repl/repl.nix b/packages/repl/repl.nix index 7f14583e..b7821c25 100644 --- a/packages/repl/repl.nix +++ b/packages/repl/repl.nix @@ -1,49 +1,31 @@ { - flakePath ? null, + flakePath, hostnamePath ? "/etc/hostname", - registryPath ? /etc/nix/registry.json, }: let - inherit (builtins) getFlake head match currentSystem readFile pathExists filter fromJSON; + inherit (builtins) currentSystem getFlake head match pathExists readFile removeAttrs; - selfFlake = - if pathExists registryPath - then filter (it: it.from.id == "self") (fromJSON (readFile registryPath)).flakes - else []; - - flakePath' = - toString - ( - if flakePath != null - then flakePath - else if selfFlake != [] - then (head selfFlake).to.path - else "/etc/nixos" - ); - - flake = - if pathExists flakePath' - then getFlake flakePath' - else {}; hostname = if pathExists hostnamePath then head (match "([a-zA-Z0-9\\-]+)\n" (readFile hostnamePath)) else ""; - nixpkgsFromInputsPath = flake.inputs.nixpkgs.outPath or ""; - nixpkgs = - flake.pkgs.${currentSystem}.nixpkgs - or ( - if nixpkgsFromInputsPath != "" - then import nixpkgsFromInputsPath {} - else {} - ); + self = + if pathExists flakePath + then + removeAttrs (getFlake (toString flakePath)) [ + # If you use flakegen, these take a lot of space + "nextFlake" + "nextFlakeSource" + ] + else {}; - nixpkgsOutput = removeAttrs (nixpkgs // nixpkgs.lib or {}) ["options" "config"]; + pkgs = self.inputs.nixpkgs.legacyPackages.${currentSystem} or {}; + lib = + if pkgs != {} + then {inherit (pkgs) lib;} + else {}; in - {inherit flake;} - // flake - // builtins - // (flake.nixosConfigurations or {}) - // flake.nixosConfigurations.${hostname} or {} - // nixpkgsOutput - // {getFlake = path: getFlake (toString path);} + {inherit lib pkgs self;} + // self.nixosConfigurations.${hostname} + or self.nixOnDroidConfigurations.default + or {}