Skip to content

Instantly share code, notes, and snippets.

@ChadSki
Last active December 26, 2023 07:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ChadSki/926e5633961c9b48131eabd32e57adb2 to your computer and use it in GitHub Desktop.
Save ChadSki/926e5633961c9b48131eabd32e57adb2 to your computer and use it in GitHub Desktop.
Nix flake shell with conda and cuda in an FHS user environment

Run NIXPKGS_ALLOW_UNFREE=1 nix develop --impure to enter the shell, then conda update conda.

{
"nodes": {
"home-manager": {
"inputs": {
"nixpkgs": "nixpkgs",
"utils": "utils"
},
"locked": {
"lastModified": 1679394816,
"narHash": "sha256-1V1esJt2YAxsKmRuGuB62RF5vhDAVFDvJXVNhtEO22A=",
"owner": "nix-community",
"repo": "home-manager",
"rev": "e386ec640e16dc91120977285cb8c72c77078164",
"type": "github"
},
"original": {
"id": "home-manager",
"type": "indirect"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1677932085,
"narHash": "sha256-+AB4dYllWig8iO6vAiGGYl0NEgmMgGHpy9gzWJ3322g=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "3c5319ad3aa51551182ac82ea17ab1c6b0f0df89",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1679262748,
"narHash": "sha256-DQCrrAFrkxijC6haUzOC5ZoFqpcv/tg2WxnyW3np1Cc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "60c1d71f2ba4c80178ec84523c2ca0801522e0a6",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"home-manager": "home-manager",
"nixpkgs": "nixpkgs_2"
}
},
"utils": {
"locked": {
"lastModified": 1676283394,
"narHash": "sha256-XX2f9c3iySLCw54rJ/CZs+ZK6IQy7GXNY4nSOyu2QG4=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "3db36a8b464d0c4532ba1c7dda728f4576d6d073",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
{
description = "An FHS shell with conda and cuda.";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs, home-manager }:
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
# Conda installs it's packages and environments under this directory
installationPath = "~/.conda";
# Downloaded Miniconda installer
minicondaScript = pkgs.stdenv.mkDerivation rec {
name = "miniconda-${version}";
version = "4.3.11";
src = pkgs.fetchurl {
url = "https://repo.continuum.io/miniconda/Miniconda3-${version}-Linux-x86_64.sh";
sha256 = "1f2g8x1nh8xwcdh09xcra8vd15xqb5crjmpvmc2xza3ggg771zmr";
};
# Nothing to unpack.
unpackPhase = "true";
# Rename the file so it's easier to use. The file needs to have .sh ending
# because the installation script does some checks based on that assumption.
# However, don't add it under $out/bin/ becase we don't really want to use
# it within our environment. It is called by "conda-install" defined below.
installPhase = ''
mkdir -p $out
cp $src $out/miniconda.sh
'';
# Add executable mode here after the fixup phase so that no patching will be
# done by nix because we want to use this miniconda installer in the FHS
# user env.
fixupPhase = ''
chmod +x $out/miniconda.sh
'';
};
# Wrap miniconda installer so that it is non-interactive and installs into the
# path specified by installationPath
conda = pkgs.runCommand "conda-install"
{ buildInputs = [ pkgs.makeWrapper minicondaScript ]; }
''
mkdir -p $out/bin
makeWrapper \
${minicondaScript}/miniconda.sh \
$out/bin/conda-install \
--add-flags "-p ${installationPath}" \
--add-flags "-b"
'';
in {
devShell.x86_64-linux = (pkgs.buildFHSUserEnv {
name = "conda";
targetPkgs = pkgs: (
with pkgs; [
autoconf
binutils
conda
cudatoolkit
curl
freeglut
gcc11
git
gitRepo
gnumake
gnupg
gperf
libGLU libGL
libselinux
linuxPackages.nvidia_x11
m4
ncurses5
procps
stdenv.cc
unzip
util-linux
wget
xorg.libICE
xorg.libSM
xorg.libX11
xorg.libXext
xorg.libXi
xorg.libXmu
xorg.libXrandr
xorg.libXrender
xorg.libXv
zlib
]
);
profile = ''
# cuda
export CUDA_PATH=${pkgs.cudatoolkit}
# export LD_LIBRARY_PATH=${pkgs.linuxPackages.nvidia_x11}/lib
export EXTRA_LDFLAGS="-L/lib -L${pkgs.linuxPackages.nvidia_x11}/lib"
export EXTRA_CCFLAGS="-I/usr/include"
# conda
export PATH=${installationPath}/bin:$PATH
# Paths for gcc if compiling some C sources with pip
export NIX_CFLAGS_COMPILE="-I${installationPath}/include"
export NIX_CFLAGS_LINK="-L${installationPath}lib"
# Some other required environment variables
export FONTCONFIG_FILE=/etc/fonts/fonts.conf
export QTCOMPOSE=${pkgs.xorg.libX11}/share/X11/locale
'';
}).env;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment