Skip to content

Instantly share code, notes, and snippets.

@andir
Last active October 22, 2020 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andir/efde0580731ec7cbee1360d29dfeb03b to your computer and use it in GitHub Desktop.
Save andir/efde0580731ec7cbee1360d29dfeb03b to your computer and use it in GitHub Desktop.
linotp vscode

VS Code LinOTP setup with Nix

Install the following extensions & make sure the settings (settings.json) are set.

  • Nix Environment Selector (arrterian.nix-env-selector)
  • Python (ms-python.python)

Put the given shell.nix into the linotp checkout.

Open the project in VS Code & select the Nix environment. It should automatically select the right file (since we changed the default).

At some point during this setup you will be asked to configure a python linter. Both flake8 and mypy are avilable, select what makes sense for you.

Open a new termin (from within VS code) and enter the nix-shell (that isn't yet automagic: arrterian/nix-env-selector#31). Within the nix-shell create a new virtual env python -m venv venv. VS Code should have recognized the new folder and picked that up as the projects Python interpreter.

Now, still in the very same shell, activate the venv via source venv/bin/activate. Now you can go ahead an install the linotp dependencies as normal cd linotpd/src/; pip install -e '.[test]'; python setup.py develop.

After installing all the develop dependencies you should be able to run linotp in the active shell.

In the debug section you can create a new launch configuration, copy the given template and it should just work(tm).

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"cwd": "${workspaceFolder}/linotpd/src",
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "linotpd/src/linotpapp.py",
"FLASK_ENV": "development",
"FLASK_DEBUG": "0",
"LD_LIBRARY_PATH": "${env:HOME}/.cache/linotp-libs/lib",
"DYLD_FALLBACK_LIBRARY_PATH": "${env:HOME}/.cache/linotp-libs/lib"
},
"args": [
"run",
"--no-debugger"
],
"jinja": true
}
]
}
{
"nixEnvSelector.nixShellConfig": "${workspaceRoot}/shell.nix",
}
let
# this pins all of this to one specific verison of nixpkgs
# You can pick any commit from the referenced repository, usually you want the latest of a given branch
pkgs = import (builtins.fetchTarball https://github.com/NixOS/nixpkgs-channels/archive/1179840f9a88b8a548f4b11d1a03aa25a790c379.tar.gz) {};
python = pkgs.python38;
# There are all the packages you commonly need when working on this project, these might not be declared in setup.py
buildInputs = [
pkgs.nix
pkgs.openssl
# python stuff
python
python.pkgs.pip
python.pkgs.flake8
python.pkgs.setuptools
pkgs.mypy
pkgs.black
# native dependencies
pkgs.pkgconfig
pkgs.openldap
pkgs.cyrus_sasl
pkgs.libsodium
];
# symlink pool for all the runtime discoverable native libs
libs = pkgs.buildEnv {
name = "linotp-native-libs";
paths = [
pkgs.libsodium
];
};
# symlink pool that we use to create a gc root for all our packages
shellRoot = pkgs.buildEnv {
name = "linotp-shell-gcroot";
paths = buildInputs;
};
in pkgs.mkShell {
inherit buildInputs;
# we use the shellHook to execute a few commands whenever we enter the shell.
# 1. we export LD_LIBRARY_PATH so that pysodium is able to find libsodium as it isn't available system-wide
# 2. we register a few Nix GC roots so our venv continues working (until we decide to upgrade it) even when the system did GC
shellHook = ''
export LD_LIBRARY_PATH="${libs}/lib"
export DYLD_FALLBACK_LIBRARY_PATH="${libs}/lib"
nix-store --add-root ~/.cache/linotp-libs --indirect -r ${libs}
nix-store --add-root ~/.cache/linotp-shell --indirect -r ${shellRoot}
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment