Skip to content

Instantly share code, notes, and snippets.

@pmenke-de
Forked from chpatrick/nix-cmake
Last active May 10, 2024 09:54
Show Gist options
  • Save pmenke-de/2fed80213c48c2fe80891678f4fa3b42 to your computer and use it in GitHub Desktop.
Save pmenke-de/2fed80213c48c2fe80891678f4fa3b42 to your computer and use it in GitHub Desktop.
Using CLion with Nix

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.

  1. create a .nix utility directory in your project directory.
  2. put the below nix-run.sh and nix-cmake.sh in the .nix directory.
  3. in the .nix directory create symlinks for make, gcc, g++ - and maybe more tools, that need to have the nix dependencies and build tools available - and point them to nix-run.sh
  4. then, in Settings -> Build, Execution, Deployment -> Toolchains set CMake to the path to nix-cmake.sh and point all other build tools to the symlinks you've created.
#!/usr/bin/env sh
# Use the cmakeFlags set by Nix - this doesn't work with --build
FLAGS=$(echo "$@" | grep -e '--build' > /dev/null || echo "$cmakeFlags")
"$(dirname "$0")"/nix-run.sh cmake ${FLAGS:+"$FLAGS"} "$@"
#!/usr/bin/env sh
# support arbitrary commands trough symlinks to this file
# e.g. create a symlink "make" to "nix-run.sh" and execution
# of the symlink will run "make" in the nix-shell environment
if [ "$(basename "$0")" != "nix-run.sh" ]; then
CMD="$(basename "$0")"
fi
# we need to check, if we're already running inside the
# nix-shell environment, as some nix-run.sh symlinks may
# call other nix-run.sh symlinks, which wouldn't work.
# this is because nix-shell itself will not be available
# when run in --pure mode
if [ -n "$IN_NIX_RUN" ]; then
exec "$CMD" "$@"
else
#relative path containing default.nix / shell.nix
PROJECT_DIR=$(dirname "$0")/..
QUOT_ARGS=$(printf '"%s" ' "$@")
IN_NIX_RUN=1 nix-shell --pure --keep IN_NIX_RUN --run "$CMD $QUOT_ARGS" "$PROJECT_DIR"
exit $?
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment