From cf3bb8870d056cf535593986d90c54e901ea4512 Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Thu, 9 Oct 2025 15:46:24 +0900 Subject: [PATCH] feat(nix): add flake templates Signed-off-by: Seongmin Lee --- nix/flake.nix | 2 ++ nix/templates/python/flake.nix | 48 ++++++++++++++++++++++++++++++++++ nix/templates/templates.nix | 18 +++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 nix/templates/python/flake.nix create mode 100644 nix/templates/templates.nix diff --git a/nix/flake.nix b/nix/flake.nix index ce76909..223c519 100644 --- a/nix/flake.nix +++ b/nix/flake.nix @@ -118,6 +118,8 @@ pkgs.slack ] else []); }; + # This can be just `imports = [ ./templates/templates.nix ]` when using flake-parts + templates = ((import ./templates/templates.nix) { inherit inputs; }).flake.templates; }; } # vim: et:ts=2 diff --git a/nix/templates/python/flake.nix b/nix/templates/python/flake.nix new file mode 100644 index 0000000..b171f72 --- /dev/null +++ b/nix/templates/python/flake.nix @@ -0,0 +1,48 @@ +{ + description = "python flake"; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + }; + outputs = inputs @ { flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ + "x86_64-linux" + "aarch64-linux" + "aarch64-darwin" + ]; + perSystem = { pkgs, system, ... }: let + commonPackages = [ + pkgs.python313Full + pkgs.pyright + pkgs.uv + pkgs.curl + pkgs.wget + pkgs.cmake + ] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [ + pkgs.gcc + pkgs.stdenv.cc.cc.lib + ]; + in { + devShells.default = pkgs.mkShell { + buildInputs = commonPackages; + shellHook = '' + # Set up the Python virtual environment with uv + test -d .venv || ${pkgs.uv}/bin/uv venv .venv + export VIRTUAL_ENV="$(pwd)/.venv" + export PATH="$VIRTUAL_ENV/bin:$PATH" + export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath commonPackages}:$LD_LIBRARY_PATH + + source "$VIRTUAL_ENV/bin/activate" + + if uv sync --frozen; then + package_count=$(uv pip list --format=freeze | wc -l) + echo "Done. $package_count pip packages installed." + else + echo "Warning: An error occurred during uv sync." + fi + ''; + }; + }; + }; +} diff --git a/nix/templates/templates.nix b/nix/templates/templates.nix new file mode 100644 index 0000000..7548cd0 --- /dev/null +++ b/nix/templates/templates.nix @@ -0,0 +1,18 @@ +{ inputs, ... }: +let templatesDir = ./.; +in { + flake.templates = let + mkTemplate = name: val: + let + path = templatesDir + "/${name}"; + flake = import (path + "/flake.nix"); + in { + inherit path; + description = flake.description; + }; + templateEntities = let + op = _: val: val == "directory"; + as = builtins.readDir templatesDir; + in inputs.nixpkgs.lib.filterAttrs op as; + in builtins.mapAttrs mkTemplate templateEntities; +} -- 2.51.2