From dd3fd7dce623cfb026c655426083719eb8b44010 Mon Sep 17 00:00:00 2001 From: Alexander Heldt Date: Sat, 11 Nov 2023 23:41:55 +0100 Subject: [PATCH] Rework `skal` to be the shared `nix-wrapper` module --- hosts/pinwheel/configuration.nix | 9 +- hosts/pinwheel/modules/skal/default.nix | 59 ---------- nix-wrapper/default.nix | 101 ++++++++++++++++++ .../skal => nix-wrapper}/flake-template.nix | 14 ++- 4 files changed, 115 insertions(+), 68 deletions(-) delete mode 100644 hosts/pinwheel/modules/skal/default.nix create mode 100644 nix-wrapper/default.nix rename {hosts/pinwheel/modules/skal => nix-wrapper}/flake-template.nix (65%) diff --git a/hosts/pinwheel/configuration.nix b/hosts/pinwheel/configuration.nix index c52e452..29a774f 100644 --- a/hosts/pinwheel/configuration.nix +++ b/hosts/pinwheel/configuration.nix @@ -3,6 +3,7 @@ imports = [ ../../config-manager/default.nix + ../../nix-wrapper/default.nix ../../shared-modules/syncthing.nix ./hardware-configuration.nix ./modules @@ -74,11 +75,11 @@ flakePath = "/home/alex/config"; }; - mod = { - skal = { - path = "/home/alex/code/own/skal"; - }; + nix-wrapper = { + flakesPath = "/home/alex/code/own/flakes"; + }; + mod = { nix-index.enable = false; greetd.enable = true; hyprland.enable = true; diff --git a/hosts/pinwheel/modules/skal/default.nix b/hosts/pinwheel/modules/skal/default.nix deleted file mode 100644 index eab9f34..0000000 --- a/hosts/pinwheel/modules/skal/default.nix +++ /dev/null @@ -1,59 +0,0 @@ -{ pkgs, lib, config, ... }: -let - skalPath = config.mod.skal.path; - - skal = - if skalPath == "" then - throw "'skal.path' cannot be empty" - else - pkgs.writeShellScriptBin "skal" '' - URL=`git config --get remote.origin.url` - GITHOST=`echo $URL | sed -e s/.*@// | sed -e s/:.*//` - GITPATH=`echo $URL | sed -e s/.*:// | sed -e s/.git$//` - - FLAKE_DIR="${skalPath}/$GITHOST/$GITPATH" - - if [ "$1" == "create" ]; then - if [ ! -d "$FLAKE_DIR" ]; then - PWD=$(pwd) - echo Creating flake '"$FLAKE_DIR' using '$PWD' as source path" - mkdir -p "$FLAKE_DIR" - - FLAKE="$FLAKE_DIR"/flake.nix - cp ${./flake-template.nix} "$FLAKE" - chmod 700 "$FLAKE" - sed -i -e "s|SRC_PATH|$PWD|" "$FLAKE" - - ${pkgs.vim}/bin/vim "$FLAKE" - exit 0 - else - echo Flake already exist - fi - fi - - if [ ! -d $FLAKE_DIR ]; then - echo No flake exist for "$FLAKE_DIR" - exit 1 - fi - - echo Using "$FLAKE_DIR" - nix develop $FLAKE_DIR $* - ''; -in -{ - options = { - mod.skal = { - path = lib.mkOption { - description = "path to where all flake.nix reside"; - type = lib.types.str; - default = ""; - }; - }; - }; - - config = { - home-manager.users.alex = { - home.packages = [ skal ]; - }; - }; -} diff --git a/nix-wrapper/default.nix b/nix-wrapper/default.nix new file mode 100644 index 0000000..e39a865 --- /dev/null +++ b/nix-wrapper/default.nix @@ -0,0 +1,101 @@ +{ pkgs, lib, config, ... }: +let + flakesPath = config.nix-wrapper.flakesPath; + + nix-wrapper = + if flakesPath == "" then + throw "'nix-wrapper.flakesPath' cannot be empty" + else + pkgs.writeShellScriptBin "nix-wrapper" '' + URL=`git config --get remote.origin.url` + GITHOST=`echo $URL | sed -e s/.*@// | sed -e s/:.*//` + GITPATH=`echo $URL | sed -e s/.*:// | sed -e s/.git$//` + FLAKE_PATH="${flakesPath}/$GITHOST/$GITPATH" + + usage() { + cat << EOF +Usage: + nix-wrapper [options] + +Options: + -c create a flake.nix for the current path + -e edit the flake.nix for the current path + -a target a specific attribute +EOF + } + + create() { + if [ -d "$FLAKE_PATH" ]; then + echo "flake.nix already exist at $FLAKE_PATH" + exit 1 + fi + + PWD=$(pwd) + echo "Creating flake.nix in '$FLAKE_PATH' using '$PWD' as source path" + mkdir -p "$FLAKE_PATH" + + FLAKE="$FLAKE_PATH"/flake.nix + cp ${./flake-template.nix} "$FLAKE" + chmod 700 "$FLAKE" + sed -i -e "s|./SRC_PATH|$PWD|" "$FLAKE" + + ${pkgs.vim}/bin/vim "$FLAKE" + } + + edit() { + if [ ! -d "$FLAKE_PATH" ]; then + echo "no flake.nix exist for '$FLAKE_PATH'" + exit 1 + fi + + ${pkgs.vim}/bin/vim "$FLAKE_PATH/flake.nix" + } + + [ "$#" -eq 0 ] && usage && exit 0 + + ATTR="" + while getopts "cea:" opt; do + case "$opt" in + c) + create + exit 0 ;; + e) + edit + exit 0 ;; + a) + ATTR="$OPTARG" ;; + esac + done + + shift "$((OPTIND-1))" + + if [ ! -d "$FLAKE_PATH" ]; then + echo "no flake.nix exist for '$FLAKE_PATH'" + exit 1 + fi + + [ -n "$ATTR" ] && FLAKE_PATH="$FLAKE_PATH#$ATTR" + nix $@ "$FLAKE_PATH" + ''; +in +{ + options = { + nix-wrapper = { + flakesPath = lib.mkOption { + description = "path to where all flake.nix reside"; + type = lib.types.str; + default = ""; + }; + }; + }; + + config = { + environment = { + shellAliases = { + nw = "${nix-wrapper}/bin/nix-wrapper"; + }; + + systemPackages = [ nix-wrapper ]; + }; + }; +} diff --git a/hosts/pinwheel/modules/skal/flake-template.nix b/nix-wrapper/flake-template.nix similarity index 65% rename from hosts/pinwheel/modules/skal/flake-template.nix rename to nix-wrapper/flake-template.nix index 881a74e..358b1be 100644 --- a/hosts/pinwheel/modules/skal/flake-template.nix +++ b/nix-wrapper/flake-template.nix @@ -11,13 +11,17 @@ in { packages = nixpkgs.lib.genAttrs systems (system: - # let - # pkgs = nixpkgs.legacyPackages.${system}; - # in + let + pkgs = nixpkgs.legacyPackages.${system}; + in { - default = { - src = SRC_PATH; + # `derivation` may be filled out or switched + # to something like `pkgs.buildGoModule` + default = derivation { + src = ./SRC_PATH; }; + + hello = pkgs.hello; } );