Skip to content

Instantly share code, notes, and snippets.

@chpatrick
Created September 15, 2018 11:38
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chpatrick/61a5d486dd0d806bdf07e1b1ddc85ce5 to your computer and use it in GitHub Desktop.
Save chpatrick/61a5d486dd0d806bdf07e1b1ddc85ce5 to your computer and use it in GitHub Desktop.
Using CLion with Nix
#!/usr/bin/env python3
# let's say you have a C++ project in Nix that you want to work on with CLion so that the Nix dependencies are available
# put this script in your project directory
# then, in Settings -> Build, Execution, Deployment -> Toolchains set CMake to this script
# if you need any extra nix-shell arguments, add them to the invocation at the bottom
import os
import sys
import shlex
scriptDir = os.path.dirname(os.path.realpath(__file__))
args = list(map(shlex.quote, sys.argv[1:]))
# Use the cmakeFlags set by Nix - this doesn't work with --build
if "--build" not in args:
args.insert(0, "$cmakeFlags")
cwd = os.getcwd()
cmd = 'cd ' + cwd + ' && cmake ' + ' '.join(args)
os.chdir(scriptDir)
os.execvp("nix-shell", [
'nix-shell',
'--pure',
'--run', cmd
])
@l0b0
Copy link

l0b0 commented Jan 18, 2023

Thank you! One minor issue is replacing cwd in line 21 with shlex.quote(cwd) makes it work with spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment