diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..3872b41 --- /dev/null +++ b/flake.nix @@ -0,0 +1,114 @@ +rec { + description = ""; + + inputs = { + nixpkgs.url = "nixpkgs"; # Use nixpkgs from system registry + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { + self, + nixpkgs, + flake-utils, + }: + # Create a shell (and possibly package) for each possible system, not only x86_64-linux + flake-utils.lib.eachDefaultSystem (system: let + # ========================================================================================= + # Define pkgs/stdenvs + # ========================================================================================= + pkgs = import nixpkgs { + inherit system; + config.allowUnfree = true; + overlays = []; + }; + + inherit (pkgs) lib stdenv; + + # ========================================================================================= + # Define shell environment + # ========================================================================================= + + # Setup the shell when entering the "nix develop" environment (bash script). + shellHook = let + # Add project-local fish abbrs here + abbrs = {}; + + eraseAbbr = name: value: ''abbr --erase ${name} 2>/dev/null''; + createAbbr = name: value: ''abbr -a ${name} "${value}"''; + + # This will be sourced by the global fish config if INIT_PROJECT_SHELL gets unset + unloadProjectShell = pkgs.writers.writeFish "unload-shell.fish" '' + echo "Unloading \"${description}\" environment..." + + ${builtins.concatStringsSep "\n" (lib.mapAttrsToList eraseAbbr abbrs)} + ''; + + # This will be sourced by the global fish config if INIT_PROJECT_SHELL gets set + initProjectShell = pkgs.writers.writeFish "init-shell.fish" '' + # Unload just in case, to not have redefinition errors + source ${unloadProjectShell} + + echo "Sourcing \"${description}\" environment..." + + ${builtins.concatStringsSep "\n" (lib.mapAttrsToList createAbbr abbrs)} + ''; + in + builtins.concatStringsSep "\n" [ + # Launch into pure fish shell + '' + # Determine the project root, used e.g. in cmake scripts + export FLAKE_PROJECT_ROOT="$(git rev-parse --show-toplevel)" + + # Can't do the "exec" with nix-direnv + # - The "exec fish" would call direnv again => Infinite loop + # - The shellHook is Bash/POSIX, so fish syntax doesn't work + + # Use this for "nix develop" without direnv + # exec "$(type -p fish)" -C "source ${initProjectShell} && abbr -a menu '${pkgs.bat}/bin/bat "${initProjectShell}"'" + + # Use this for direnv without "nix develop" + export INIT_PROJECT_SHELL="${initProjectShell}" + export UNLOAD_PROJECT_SHELL="${unloadProjectShell}" + '' + ]; + + # =========================================================================================== + # Define custom dependencies + # =========================================================================================== + + # =========================================================================================== + # Specify dependencies + # https://nixos.org/manual/nixpkgs/stable/#ssec-stdenv-dependencies-overview + # Just for a "nix develop" shell, buildInputs can be used for everything. + # =========================================================================================== + + # Add dependencies to nativeBuildInputs if they are executed during the build: + # - Those which are needed on $PATH during the build, for example cmake and pkg-config + # - Setup hooks, for example makeWrapper + # - Interpreters needed by patchShebangs for build scripts (with the --build flag), which can be the case for e.g. perl + nativeBuildInputs = with pkgs; []; + + # Add dependencies to buildInputs if they will end up copied or linked into the final output or otherwise used at runtime: + # - Libraries used by compilers, for example zlib + # - Interpreters needed by patchShebangs for scripts which are installed, which can be the case for e.g. perl + buildInputs = with pkgs; []; + # =========================================================================================== + # Define buildable + installable packages + # =========================================================================================== + in rec { + devShells = { + # Provide default environment for "nix develop". + # Other environments can be added below. + default = pkgs.mkShell { + inherit nativeBuildInputs buildInputs shellHook; + name = description; + + # ========================================================================================= + # Define environment variables + # ========================================================================================= + + BIBINPUTS = "$PWD:$BIBINPUTS"; + }; + }; + }); +}