Skip to content

Commit 41f95fd

Browse files
committed
Squashed commit of the following:
commit b54a13480c9da962c17481fa4b6b09a08af1ae95 Author: antares <antares@alyr.dev> Date: Fri Jul 25 22:21:51 2025 +0800 Update commit e6ff757 Author: antares <antares@alyr.dev> Date: Fri Jul 18 21:51:45 2025 +0800 Add nix files
1 parent f8218aa commit 41f95fd

8 files changed

Lines changed: 200 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
benchmark_result*.json
22
/*.pdf
3+
/.nix-pyenv
4+
/.direnv
35

46
# Byte-compiled / optimized / DLL files
57
__pycache__/

.vscode/settings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"python.analysis.stubPath": ".nix-pyenv/lib",
3+
"terminal.integrated.profiles.linux": {
4+
"nix-shell": {
5+
"path": "/run/current-system/sw/bin/nix-shell",
6+
"icon": "terminal-bash"
7+
},
8+
"nix-dev": {
9+
"path": "/run/current-system/sw/bin/nix",
10+
"args": ["develop", "-c", "nix-zshell"],
11+
"icon": "terminal-bash"
12+
}
13+
},
14+
"terminal.integrated.defaultProfile.linux": "nix-dev"
15+
}

flake.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
description = "A simple flake for a simple python environment";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
};
7+
8+
outputs =
9+
{ self, nixpkgs }:
10+
let
11+
forAllSystems =
12+
function:
13+
nixpkgs.lib.genAttrs
14+
[
15+
"x86_64-linux"
16+
"aarch64-linux"
17+
"x86_64-darwin"
18+
"aarch64-darwin"
19+
]
20+
(
21+
system:
22+
function (
23+
import nixpkgs {
24+
inherit system;
25+
}
26+
)
27+
);
28+
in
29+
{
30+
devShells = forAllSystems (pkgs: rec {
31+
default = pkgs.callPackage ./shell.nix { persist = true; };
32+
});
33+
};
34+
}

py_requirements.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{ pkgs, ... }:
2+
pypkgs: with pypkgs; [
3+
matplotlib
4+
orjson
5+
psutil
6+
reportlab
7+
svglib
8+
build
9+
pip
10+
]

run_benchmark.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python benchmark.py

shell.nix

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
pkgs ? import <nixpkgs> { },
3+
lib ? pkgs.lib,
4+
persist ? false,
5+
mkShell ? pkgs.mkShell,
6+
callPackage ? pkgs.callPackage,
7+
}:
8+
let
9+
optionalAttrs = lib.attrsets.optionalAttrs;
10+
# define the nix-pyenv directory
11+
nix-pyenv-directory = ".nix-pyenv";
12+
# define version
13+
usingPython = pkgs.python313;
14+
# import required python packages
15+
requiredPythonPackages = callPackage ./py_requirements.nix { };
16+
# create python environment
17+
pyenv = usingPython.withPackages requiredPythonPackages;
18+
#
19+
callShellHookParam = {
20+
inherit
21+
nix-pyenv-directory
22+
pyenv
23+
usingPython
24+
persist
25+
pkgs
26+
;
27+
};
28+
internalShell = mkShell (
29+
{
30+
packages = [
31+
pyenv
32+
pkgs.cmake
33+
];
34+
}
35+
// (optionalAttrs (!persist) {
36+
shellHook = callPackage ./shellhook.nix callShellHookParam;
37+
})
38+
);
39+
in
40+
internalShell.overrideAttrs (
41+
optionalAttrs persist {
42+
shellHook = callPackage ./shellhook.nix (
43+
callShellHookParam
44+
// {
45+
inherit (internalShell) inputDerivation;
46+
}
47+
);
48+
}
49+
)

shellhook.nix

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
nix,
3+
nix-pyenv-directory,
4+
pyenv,
5+
usingPython,
6+
persist,
7+
inputDerivation ? "",
8+
...
9+
}:
10+
let
11+
optionalCommentOut = if persist then "" else "#";
12+
sitePackages = usingPython.sitePackages;
13+
in
14+
''
15+
if [[ $name == nix-shell ]]; then
16+
cd ${builtins.toString ./.}
17+
fi
18+
19+
# ensure the nix-pyenv directory exists
20+
if [[ ! -d ${nix-pyenv-directory} ]]; then mkdir ${nix-pyenv-directory}; fi
21+
if [[ ! -d ${nix-pyenv-directory}/lib ]]; then mkdir ${nix-pyenv-directory}/lib; fi
22+
if [[ ! -d ${nix-pyenv-directory}/bin ]]; then mkdir ${nix-pyenv-directory}/bin; fi
23+
24+
ensure_symlink() {
25+
local link_path="$1"
26+
local target_path="$2"
27+
if [[ -L "$link_path" ]] && [[ "$(readlink "$link_path")" = "$target_path" ]]; then
28+
return 0
29+
fi
30+
rm -f "$link_path" > /dev/null 2>&1
31+
ln -s "$target_path" "$link_path"
32+
}
33+
34+
# creating python library symlinks
35+
for file in ${pyenv}/${sitePackages}/*; do
36+
basefile=$(basename $file)
37+
if [ -d "$file" ]; then
38+
if [[ "$basefile" != *dist-info && "$basefile" != __pycache__ ]]; then
39+
ensure_symlink "${nix-pyenv-directory}/lib/$basefile" $file
40+
fi
41+
else
42+
# the typing_extensions.py will make the vscode type checker not working!
43+
if [[ $basefile == *.so ]] || ([[ $basefile == *.py ]] && [[ $basefile != typing_extensions.py ]]); then
44+
ensure_symlink "${nix-pyenv-directory}/lib/$basefile" $file
45+
fi
46+
fi
47+
done
48+
for file in ${nix-pyenv-directory}/lib/*; do
49+
if [[ -L "$file" ]] && [[ "$(dirname $(readlink "$file"))" != "${pyenv}/${sitePackages}" ]]; then
50+
rm -f "$file"
51+
fi
52+
done
53+
54+
# ensure the typing_extensions.py is not in the lib directory
55+
rm ${nix-pyenv-directory}/lib/typing_extensions.py > /dev/null 2>&1
56+
57+
# add python executable to the bin directory
58+
ensure_symlink "${nix-pyenv-directory}/bin/python" ${pyenv}/bin/python
59+
export PATH=${nix-pyenv-directory}/bin:$PATH
60+
61+
${optionalCommentOut} ${nix}/bin/nix-store --add-root ${nix-pyenv-directory}/.nix-shell-inputs --realise ${inputDerivation}
62+
''

0 commit comments

Comments
 (0)