commit d26d4d563c5ab024c1c6bb5c1432a4d9ae5e902e Author: yukkop Date: Sat Dec 28 16:26:10 2024 +0000 init diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2a5fc97 --- /dev/null +++ b/flake.lock @@ -0,0 +1,26 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1735402525, + "narHash": "sha256-0RzBF9sKEjHo7Sinrs8wFktrUNc90ziOI48RLtZTW6U=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0429fa5b552a5c182e23d0e14de644db867961e4", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..b550507 --- /dev/null +++ b/flake.nix @@ -0,0 +1,107 @@ +{ + description = "yukkop's nix utilities"; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + }; + + outputs = { self, nixpkgs }: + let + lib = nixpkgs.lib; + recursiveUpdate = lib.recursiveUpdate; + + supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" ]; + + forSpecSystemsWithPkgs = supportedSystems: pkgOverlays: f: + builtins.foldl' (acc: system: + let + pkgs = import nixpkgs { + inherit system; + overlays = pkgOverlays; + }; + systemOutputs = f { system = system; pkgs = pkgs; }; + in + recursiveUpdate acc systemOutputs + ) {} supportedSystems; + + forAllSystemsWithPkgs = pkgOverlays: f: forSpecSystemsWithPkgs supportedSystems pkgOverlays f; + + envErrorMessage = varName: "Error: The ${varName} environment variable is not set."; + + parseEnv = file: let + lines = builtins.filter (line: builtins.match "^var=.*" line != null) (builtins.readFile file); + attributes = builtins.listToAttrs (builtins.map (line: let + parts = builtins.split "=" line; + key = builtins.substring 0 (builtins.stringLength parts[0] - 3) parts[0]; # Remove "var" prefix + value = parts[1]; + in { + name = key; + value = value; + }) lines); + in attributes; + + dotEnv = builtins.getEnv "DOTENV"; + minorEnvironment = + if dotEnv != "" then + if builtins.pathExists dotEnv then + parseEnv dotEnv + else + throw "${dotEnv} file not exist" + else + if builtins.pathExists ./.env then + parseEnv ./.env + else + {}; + in + forAllSystemsWithPkgs [] ({ system, pkgs }: + { + packages.${system} = { + # necessary to load every time .nvimrc + # makes some magic to shading nvim but still uses nvim that shaded + nvim-alias = pkgs.writeShellScriptBin "nvim" '' + # Source .env file + if [ -f .env ]; then + set -a + . .env + set +a + fi + + # Get the directory of this script + SCRIPT_DIR=$(dirname "$(readlink -f "$0")") + + # Remove the script's directory from PATH to avoid recursion + PATH=$(echo "$PATH" | tr ':' '\n' | grep -v "$SCRIPT_DIR" | paste -sd ':' -) + + # Find the system's nvim + SYSTEM_NVIM=$(command -v nvim) + + if [ -z "$SYSTEM_NVIM" ]; then + echo "Error: nvim not found in PATH" >&2 + exit 1 + fi + + # Execute the system's nvim with your custom arguments + exec "$SYSTEM_NVIM" --cmd 'lua vim.o.exrc = true' "$@" + ''; + }; + }) // { + lib = { + # -- For all systems -- + inherit forAllSystemsWithPkgs forSpecSystemsWithPkgs; + + # -- Env processing -- + getEnv = varName: let + var = builtins.getEnv varName; + in + if var != "" then + var + else if minorEnvironment ? varName then + minorEnvironment."${varName}" + else + throw (envErrorMessage varName); + + # -- Cargo.toml -- + getVersion = src: (builtins.fromTOML (builtins.readFile "${src}/Cargo.toml")).package.version; + + }; + }; +}