diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..14f9bae --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,11 @@ +# DO NOT MODIFY +# Generated by `nix run .#update-pre-commit-config` +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.15.20 + hooks: + - id: ruff-check + args: [--fix] + types_or: [python, pyi] + - id: ruff-format + types_or: [python, pyi] diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..899b2b3 --- /dev/null +++ b/flake.lock @@ -0,0 +1,140 @@ +{ + "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1767039857, + "narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=", + "owner": "NixOS", + "repo": "flake-compat", + "rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": [ + "systems" + ] + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "git-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1783008725, + "narHash": "sha256-jGiy6+sxjNWXSjp25uoJuNfyH9zBK1PEDY0lVoL4ibQ=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "bca82caa46d5ec0f5d422c61fb1e30bc51313cbe", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1784007870, + "narHash": "sha256-djcLt/JJphyNt4eDY9XTly+/WbCK5lqWq9lSgCmJkkQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "18b9261cb3294b6d2a06d03f96872827b8fe2698", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_master": { + "locked": { + "lastModified": 1774887359, + "narHash": "sha256-O32w8cY3AxXcg5+6tmVoPVkVMc9J0/enPWpA+RK0icI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "389a39c40e0368903aef0db781807bb9f6bced8b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "master", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "git-hooks": "git-hooks", + "nixpkgs": "nixpkgs", + "nixpkgs_master": "nixpkgs_master", + "systems": "systems", + "treefmt-nix": "treefmt-nix" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1780220602, + "narHash": "sha256-eynAfOmbmxJnkp7YewvCEbShNnnYJ9gLLqkzsYtBPeM=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "db947814a175b7ca6ded66e21383d938df01c227", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..ddf023a --- /dev/null +++ b/flake.nix @@ -0,0 +1,154 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + nixpkgs_master.url = "github:NixOS/nixpkgs/master"; + systems.url = "github:nix-systems/default"; + flake-utils.url = "github:numtide/flake-utils"; + flake-utils.inputs.systems.follows = "systems"; + git-hooks = { + url = "github:cachix/git-hooks.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = + { + self, + nixpkgs, + flake-utils, + git-hooks, + treefmt-nix, + ... + }@inputs: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + config.allowUnfree = true; + config.cudaSupport = true; + }; + + mpkgs = import inputs.nixpkgs_master { + inherit system; + config.allowUnfree = true; + config.cudaSupport = true; + }; + + libList = [ + # Add needed packages here + mpkgs.stdenv.cc.cc.lib + pkgs.libGL + pkgs.glib + pkgs.zlib + ] + ++ pkgs.lib.optionals pkgs.stdenv.isLinux (with pkgs; [ ]); + + treefmtEval = treefmt-nix.lib.evalModule pkgs { + projectRootFile = "flake.nix"; + programs.nixfmt.enable = true; + programs.ruff-check.enable = true; + programs.ruff-format.enable = true; + }; + + portablePreCommitConfig = pkgs.writeText "pre-commit-config.yaml" '' + # DO NOT MODIFY + # Generated by `nix run .#update-pre-commit-config` + repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v${pkgs.ruff.version} + hooks: + - id: ruff-check + args: [--fix] + types_or: [python, pyi] + - id: ruff-format + types_or: [python, pyi] + ''; + + updatePreCommitConfig = pkgs.writeShellApplication { + name = "update-pre-commit-config"; + runtimeInputs = [ + pkgs.coreutils + pkgs.git + ]; + text = '' + repoRoot="$(git rev-parse --show-toplevel)" + rm -f "$repoRoot/.pre-commit-config.yaml" + install -m 0644 ${portablePreCommitConfig} "$repoRoot/.pre-commit-config.yaml" + ''; + }; + + pre-commit-check = git-hooks.lib.${system}.run { + src = ./.; + package = pkgs.prek; + install.enable = false; + hooks.treefmt = { + enable = true; + package = treefmtEval.config.build.wrapper; + }; + }; + in + with pkgs; + { + apps.update-pre-commit-config = { + type = "app"; + program = "${updatePreCommitConfig}/bin/update-pre-commit-config"; + meta.description = "Regenerate the portable pre-commit configuration"; + }; + checks = { + inherit pre-commit-check; + formatting = treefmtEval.config.build.check self; + pre-commit-config = runCommand "pre-commit-config-check" { nativeBuildInputs = [ diffutils ]; } '' + diff --unified ${portablePreCommitConfig} ${self}/.pre-commit-config.yaml + touch $out + ''; + }; + formatter = treefmtEval.config.build.wrapper; + devShells = { + default = + let + python_with_pkgs = pkgs.python311.withPackages (pp: [ + # Add python pkgs here that you need from nix repos + ]); + in + mkShell { + NIX_LD = runCommand "ld.so" { } '' + ln -s "$(cat '${pkgs.stdenv.cc}/nix-support/dynamic-linker')" $out + ''; + NIX_LD_LIBRARY_PATH = lib.makeLibraryPath libList; + packages = [ + python_with_pkgs + python311Packages.venvShellHook + mpkgs.uv + prek + + ] + ++ libList; + venvDir = "./.venv"; + postVenvCreation = '' + unset SOURCE_DATE_EPOCH + ''; + postShellHook = '' + unset SOURCE_DATE_EPOCH + ''; + shellHook = '' + if ${pkgs.git}/bin/git rev-parse --git-dir > /dev/null 2>&1; then + repoRoot="$(${pkgs.git}/bin/git rev-parse --show-toplevel)" + (cd "$repoRoot" && prek install --config .pre-commit-config.yaml) + fi + + export LD_LIBRARY_PATH=$NIX_LD_LIBRARY_PATH:$LD_LIBRARY_PATH + export PYTHON_KEYRING_BACKEND=keyring.backends.fail.Keyring + runHook venvShellHook + uv sync --frozen --all-extras --dev + export PYTHONPATH=${python_with_pkgs}/${python_with_pkgs.sitePackages}:$PYTHONPATH + ''; + }; + }; + } + ); +}