Skip to content

Commit 8cd1eb4

Browse files
wd15guyertkphd
authored
switch nix recipe to flake (#992)
* switch nix recipe to flake * fix notebook output to be consistent Changes required as running the notebook live and running notebook with nbval resulted in different outputs. * update github ci to use flake * update nix installation instructions * add nix environment logging --------- Co-authored-by: Jonathan Guyer <[email protected]> Co-authored-by: Trevor Keller <[email protected]>
1 parent 3ae5bee commit 8cd1eb4

File tree

9 files changed

+198
-123
lines changed

9 files changed

+198
-123
lines changed

.github/workflows/nix.yml

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
name: nix
2+
name: Test Nix
33
on: [push, pull_request]
44
jobs:
55
test:
@@ -11,12 +11,22 @@ jobs:
1111
run:
1212
working-directory: ./
1313
steps:
14-
- uses: actions/checkout@v2.3.4
15-
- uses: cachix/install-nix-action@v14.1
14+
- uses: actions/checkout@v3
15+
- uses: cachix/install-nix-action@v20
1616
with:
1717
nix_path: nixpkgs=channel:nixos-unstable
1818
name: fipy
1919
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
2020
extraPullNames: nix-community
21-
- run: nix-shell --pure --command "python setup.py test"
22-
- run: nix-shell --pure --command "py.test --nbval --sanitize-with examples/sanitize.cfg examples/index.ipynb"
21+
- run: echo "UID=${UID}" >> $GITHUB_ENV
22+
- uses: actions/cache/restore@v3
23+
with:
24+
path: /run/user/${{ env.UID }}/http_cache.sqlite
25+
key: ${{ runner.os }}-http_cache.sqlite
26+
- run: nix develop --command bash -c "python setup.py test"
27+
- run: nix develop --command bash -c "py.test --nbval --nbval-sanitize-with examples/sanitize.cfg examples/index.ipynb"
28+
- uses: actions/cache/save@v3
29+
if: always()
30+
with:
31+
path: /run/user/${{ env.UID }}/http_cache.sqlite
32+
key: ${{ runner.os }}-http_cache.sqlite

docs/source/NIX-README.rst

+7-44
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,27 @@ Nix Installation
44
:term:`FiPy` now has a `Nix`_ expression for installing :term:`FiPy`
55
using `Nix`_. `Nix`_ is a powerful package manager for Linux and other
66
Unix systems that makes package management reliable and
7-
reproducible. The recipe works on both Linux and Mac OS X.
8-
9-
Getting Started with Nix
10-
------------------------
11-
12-
There are a number of tutorials on getting started with `Nix`_. The
13-
page that I used when getting started is on the Blog of the HPC team
14-
of GRICAD,
15-
16-
https://gricad.github.io/calcul/nix/tuto/2017/07/04/nix-tutorial.html
17-
18-
I also made my own notes,
19-
20-
https://github.com/wd15/nixes/blob/master/NIX-NOTES.md
21-
22-
which are a succinct steps that I use when setting up a new system with
23-
Nix.
7+
reproducible. The recipe works on both Linux and Mac OS X. Go to
8+
`nix.dev`_ to get started with Nix.
249

2510
Installing
2611
----------
2712

2813
Once you have a working Nix installation use::
2914

30-
$ nix-shell --pure
15+
$ nix develop
3116

3217
in the base :term:`FiPy` directory to install :term:`FiPy` with Python
33-
3 by default. Modify the `shell.nix` file to use another version of
34-
Python. ``nix-shell`` drops the user into a shell with a working
18+
3 by default. ``nix develop`` drops the user into a shell with a working
3519
version of :term:`FiPy`. To test your installation use::
3620

37-
$ nix-shell --pure --command "python setup.py test"
21+
$ nix develop --command bash -c "python setup.py test"
3822

3923
.. note::
4024

41-
:term:`Trilinos` is currently not available as part of the Nix
42-
:term:`FiPy` installation.
43-
44-
45-
Additional Packages
46-
-------------------
47-
48-
To install additional packages available from Nixpkgs_ include them in
49-
the `nativeBuildInputs` list in `shell.nix`.
50-
51-
52-
Using Pip
53-
---------
54-
55-
Packages unavailable from Nix can be installed using :term:`Pip`. In
56-
this case, the installation has been set up so that the Nix shell
57-
knows about a ``.local`` directory in the base :term:`FiPy` directory
58-
used by :term:`Pip` for installation. So, for example, to install the
59-
``toolz`` package from within the Nix shell use::
60-
61-
$ python -m pip install --user toolz
25+
The SciPy solvers are the only available solvers currently.
6226

63-
The ``.local`` directory will persist after the Nix shell has been
64-
closed.
6527

6628
.. _Nix: https://nixos.org/nix/
6729
.. _Nixpkgs: https://nixos.org/nixpkgs/
30+
.. _nix.dev: https://nix.dev

examples/index.ipynb

+17-8
Large diffs are not rendered by default.

examples/sanitize.cfg

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
[regex]
1+
[regex1]
22
regex: <Figure size [0-9]{3,4}x[0-9]{3,4}(?:.[0-9])? with [0-9]{1,3} Axes>
33
replace: <Figure size>
4+
5+
[regex2]
6+
regex: <fipy.viewers.matplotlibViewer.matplotlib2DGridViewer.Matplotlib2DGridViewer at .{1,}>
7+
replace: <fipy.viewers.matplotlibViewer.matplotlib2DGridViewer.Matplotlib2DGridViewer>

fipy/__init__.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ def _excepthook(*args):
7676

7777
if _log.isEnabledFor(logging.DEBUG):
7878
try:
79-
_fipy_environment["conda"] = environment.conda_info()
79+
_fipy_environment.update(environment.conda_info())
8080
except:
8181
pass
8282

8383
try:
84-
_fipy_environment["pip"] = environment.pip_info()
84+
_fipy_environment.update(environment.pip_info())
85+
except:
86+
pass
87+
88+
try:
89+
_fipy_environment.update(environment.nix_info())
8590
except:
8691
pass
8792

fipy/tools/logging/environment.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import subprocess
44
import sys
55

6-
__all__ = ["conda_info", "pip_info", "package_info", "platform_info"]
6+
__all__ = ["conda_info", "pip_info", "package_info", "platform_info", "nix_info"]
77

88
def conda_info(conda="conda"):
99
"""Collect information about conda environment.
@@ -25,6 +25,7 @@ def conda_info(conda="conda"):
2525
stdout = stdout.decode('ascii')
2626

2727
info["conda_info"] = json.loads(stdout)
28+
2829
p = subprocess.Popen([conda, "env", "export",
2930
"--name", info["conda_info"]["active_prefix_name"],
3031
"--json"],
@@ -53,7 +54,41 @@ def pip_info(python="python"):
5354
stdout, _ = p.communicate()
5455
stdout = stdout.decode('ascii')
5556

56-
return json.loads(stdout)
57+
info["pip"] = json.loads(stdout)
58+
59+
return info
60+
61+
def nix_info():
62+
"""Collect information about nix environment.
63+
64+
Returns
65+
-------
66+
dict
67+
Result of `nix derivation show .#fipy `.
68+
"""
69+
info = {}
70+
71+
# Better output would be from
72+
#
73+
# $ nix-store -q --tree $(nix-store --realize $(nix eval --raw .#fipy.drvPath))
74+
#
75+
# However, this is difficult to execute and can require another build.
76+
# Also doesn't return json.
77+
78+
p = subprocess.Popen(
79+
[
80+
"nix",
81+
"derivation",
82+
"show",
83+
".#fipy"
84+
], stdout=subprocess.PIPE)
85+
86+
stdout, _ = p.communicate()
87+
stdout = stdout.decode('ascii')
88+
89+
info["nix"] = json.loads(stdout)
90+
91+
return info
5792

5893
def package_info():
5994
"""Collect information about installed packages FiPy uses.

flake.lock

+61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
description = "Python environment for fipy";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
6+
utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, utils}: (utils.lib.eachSystem ["x86_64-linux" "x86_64-darwin" ] (system:
10+
let
11+
pkgs = nixpkgs.legacyPackages.${system};
12+
pypkgs = pkgs.python310Packages;
13+
14+
env = (pypkgs.fipy.overridePythonAttrs (old: rec {
15+
16+
src = pkgs.lib.cleanSource ./.;
17+
18+
nativeBuildInputs = with pypkgs; [
19+
pip
20+
pkgs.openssh
21+
nbval
22+
ipython
23+
ipykernel
24+
jupyterlab
25+
traitlets
26+
notebook
27+
] ++ propagatedBuildInputs;
28+
29+
propagatedBuildInputs = old.propagatedBuildInputs;
30+
31+
postShellHook = ''
32+
SOURCE_DATE_EPOCH=$(date +%s)
33+
export PYTHONUSERBASE=$PWD/.local
34+
export USER_SITE=`python -c "import site; print(site.USER_SITE)"`
35+
export PYTHONPATH=$PYTHONPATH:$USER_SITE:$(pwd)
36+
export PATH=$PATH:$PYTHONUSERBASE/bin
37+
38+
export OMPI_MCA_plm_rsh_agent=${pkgs.openssh}/bin/ssh
39+
40+
'';
41+
}));
42+
in
43+
rec {
44+
packages.fipy = env;
45+
packages.default = self.packages.${system}.fipy;
46+
devShells.default = env;
47+
}
48+
));
49+
}

shell.nix

-61
This file was deleted.

0 commit comments

Comments
 (0)