From bcf8bc240dacd04a63044624ac3822ba07dd6300 Mon Sep 17 00:00:00 2001 From: Paul Gray Date: Wed, 4 Sep 2024 08:33:03 -0400 Subject: [PATCH] Adding nix home-manager module --- flake.nix | 10 +++++-- nix/Readme.md | 33 +++++++++++++++++++++ nix/tabry-hm-module.nix | 65 +++++++++++++++++++++++++++++++++++++++++ nix/tabry-lang.nix | 27 +++++++++++++++++ 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 nix/Readme.md create mode 100644 nix/tabry-hm-module.nix create mode 100644 nix/tabry-lang.nix diff --git a/flake.nix b/flake.nix index d036a22..e854931 100644 --- a/flake.nix +++ b/flake.nix @@ -5,7 +5,9 @@ nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; }; outputs = { nixpkgs, flake-utils, ... }: - flake-utils.lib.eachDefaultSystem ( + let + tabryHmModule = import ./nix/tabry-hm-module.nix; + in flake-utils.lib.eachDefaultSystem ( system: let pkgs = nixpkgs.legacyPackages."${system}"; @@ -22,5 +24,9 @@ ]; }; } - ); + ) // { + homeModules = { + tabry = tabryHmModule; + }; + }; } diff --git a/nix/Readme.md b/nix/Readme.md new file mode 100644 index 0000000..4cb6f1d --- /dev/null +++ b/nix/Readme.md @@ -0,0 +1,33 @@ +## Tabry Nix Configurations + +### Home Manager Module + +This repository provides a home-manager (https://github.com/nix-community/home-manager) +module to make tabry easy to install and use via home manager. + +To use the home-manager module via flakes, add this module to your home-manager +configuration: + +```nix +{ + inputs = { + tabry.url = "github:evanbattaglia/tabry-rs"; + }; + outputs = { ..., tabry }: { + homeConfigurations. = { + modules = [ + tabry.homeModules.tabry + { + config.programs.tabry = { + enable = true; + enableBashIntegration = true; + tabryFiles = [ + ./zellij.tabry + ]; + }; + } + ] + } + }; +} +``` diff --git a/nix/tabry-hm-module.nix b/nix/tabry-hm-module.nix new file mode 100644 index 0000000..8609194 --- /dev/null +++ b/nix/tabry-hm-module.nix @@ -0,0 +1,65 @@ +{ config, lib, pkgs, ... }: + +# This file contains a Home Manager module that installs tabry +# and sets up tabry configuration files to be used by tabry +# +let + + cfg = config.programs.tabry; + + tabry = pkgs.callPackage ../default.nix {}; + tabryLang = pkgs.callPackage ./tabry-lang.nix { inherit tabry; }; + + # converts /nix/store/.../foo.tabry to "foo" + commandNameFromTabryFilename = fileName: + (builtins.replaceStrings [".tabry"] [""] (builtins.baseNameOf fileName)); + + mkInitFish = fileName: let + commandName = commandNameFromTabryFilename fileName; + in '' + tabry_completion_init ${commandName} + ''; + + compileTabryFiles = map tabryLang.compileTabryFile; + +in { + + options.programs.tabry = { + enable = lib.mkEnableOption "tabry, a tab completion library"; + enableFishIntegration = lib.mkEnableOption "enables fish completions"; + enableBashIntegration = lib.mkEnableOption "enables bash completions"; + tabryFiles = lib.mkOption { + type = with lib.types; listOf path; + default = []; + description = '' + *.tabry files to be compiled to completion json + ''; + }; + }; + + config = lib.mkIf cfg.enable ( + let + tabryImportsPath = builtins.concatStringsSep ":" (compileTabryFiles cfg.tabryFiles); + in { + home.packages = [tabry]; + + # for each file, compile it to json + # then add the dir to $TABRY_IMPORT_PATH + + programs.fish.shellInit = lib.mkIf cfg.enableFishIntegration ( + '' + set -x TABRY_IMPORT_PATH "${tabryImportsPath}:$TABRY_IMPORT_PATH" + ${tabry}/bin/tabry fish | source + ${builtins.concatStringsSep "\n" (map mkInitFish cfg.tabryFiles)} + '' + ); + + programs.bash.initExtra = lib.mkIf cfg.enableBashIntegration ( + '' + set -x TABRY_IMPORT_PATH "${tabryImportsPath}:$TABRY_IMPORT_PATH" + source <(${tabry}/bin/tabry bash) + '' + ); + } + ); +} \ No newline at end of file diff --git a/nix/tabry-lang.nix b/nix/tabry-lang.nix new file mode 100644 index 0000000..daa576e --- /dev/null +++ b/nix/tabry-lang.nix @@ -0,0 +1,27 @@ +{ stdenv, tabry, ... }: + let + # converts /nix/store/.../foo.tabry to "foo" + commandNameFromTabryFilename = filename: + (builtins.replaceStrings [".tabry"] [""] (builtins.baseNameOf filename)); + + formatJsonFilename = tabryFilename: + (commandNameFromTabryFilename tabryFilename) + ".json"; + + # This is a function that takes a .tabry file + # and returns a derivation that compiles that + # .tabry file into the tabry .json file + compileTabryFile = inFile: stdenv.mkDerivation { + name = "tabry-compile-file-${inFile}"; + buildPhase = '' + mkdir $out + ${tabry}/bin/tabry compile < ${inFile} > $out/${formatJsonFilename inFile} + ''; + # by default, stdenv.mkDerivation will run `make install` + # which we don't want to do here + dontInstall = true; + dontUnpack = true; + }; + + in { + inherit compileTabryFile commandNameFromTabryFilename; + }