From 43fa80a9f6f4b792d9712df6fc3a52c0deb7bfb8 Mon Sep 17 00:00:00 2001 From: fusiled Date: Thu, 18 Jun 2026 22:46:17 -0500 Subject: [PATCH] Simplify nix/clion integration Also added formatter to nix --- .gitignore | 5 ++++ README.md | 12 ++++++-- docs/guided_examples/flake.nix | 45 +++++++++++++++++----------- flake.nix | 43 +++++++++++++++----------- support/ides/clion/README.md | 11 +++++++ support/ides/clion/nix-cmake.sh | 29 ++++++++++++++++++ support/ides/clion/nix-run.sh | 19 ++++++++++++ support/ides/clion/setup-symlinks.sh | 26 ++++++++++++++++ 8 files changed, 152 insertions(+), 38 deletions(-) create mode 100644 support/ides/clion/README.md create mode 100755 support/ides/clion/nix-cmake.sh create mode 100755 support/ides/clion/nix-run.sh create mode 100755 support/ides/clion/setup-symlinks.sh diff --git a/.gitignore b/.gitignore index 07d1fb4..de30407 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,8 @@ /cmake-build-* /docs/guided_examples/*/build* /docs/guided_examples/flake.lock +# Avoid adding symlinks +/support/ides/clion/* + + +*.DS_Store diff --git a/README.md b/README.md index bb40bb2..fca3ac5 100644 --- a/README.md +++ b/README.md @@ -13,14 +13,16 @@ it is missing: a simple, easy to use library that does what you need. ## Documentation -Guides (Markdown files inside `docs` folder) and Doxygen built from the main branch are stored in **[GitHub Pages](https://fusiled.github.io/Simo/)** . - -## Get and initialize the repository +**[GitHub Pages](https://fusiled.github.io/Simo/)** stores Doxygen documentation and guides (Markdown files inside `docs` folder). +## Get the repository and build in 3 commands +Assuming [nix](https://nixos.org/learn/): ```bash git clone https://github.com/fusiled/Simo cd Simo +nix build ``` +The build is going to be accessible in the `result` folder. ## Build @@ -67,6 +69,10 @@ This can be achieved with: - Good documented code - Proper unit-testing coverage +### IDEs +You can see [this guide](./support/ides/clion/README.md) on how to use nix +binaries to build in CLion. + ## To Do - Create a collector with periodic window collection functionality - More documentation and examples diff --git a/docs/guided_examples/flake.nix b/docs/guided_examples/flake.nix index 21809e2..1fdcb4a 100644 --- a/docs/guided_examples/flake.nix +++ b/docs/guided_examples/flake.nix @@ -10,24 +10,35 @@ simo.inputs.flake-utils.follows = "flake-utils"; }; - outputs = { self, nixpkgs, flake-utils, simo }: - flake-utils.lib.eachDefaultSystem (system: + outputs = + { + self, + nixpkgs, + flake-utils, + simo, + }: + flake-utils.lib.eachDefaultSystem ( + system: let pkgs = import nixpkgs { inherit system; }; in { - devShells.default = pkgs.mkShell.override { - stdenv = pkgs.clangStdenv; - } { - packages = with pkgs; [ - boost - cmake - ninja - llvm - glaze - doxygen - simo.packages.${system}.default - ]; - }; - }); -} \ No newline at end of file + devShells.default = + pkgs.mkShell.override + { + stdenv = pkgs.clangStdenv; + } + { + packages = with pkgs; [ + boost + cmake + ninja + llvm + glaze + doxygen + simo.packages.${system}.default + ]; + }; + } + ); +} diff --git a/flake.nix b/flake.nix index 8eebf9c..003b21a 100644 --- a/flake.nix +++ b/flake.nix @@ -19,8 +19,14 @@ flake-utils.url = "github:numtide/flake-utils"; }; - outputs = { self, nixpkgs, flake-utils }: - flake-utils.lib.eachDefaultSystem (system: + outputs = + { + self, + nixpkgs, + flake-utils, + }: + flake-utils.lib.eachDefaultSystem ( + system: let pkgs = import nixpkgs { inherit system; }; simo = pkgs.clangStdenv.mkDerivation { @@ -29,6 +35,7 @@ src = ./.; nativeBuildInputs = with pkgs; [ + # Real build dependencies cmake ninja doxygen @@ -47,28 +54,28 @@ doCheck = true; checkPhase = '' - ls *.so ctest --output-on-failure ''; }; - in { + in + { packages.default = simo; checks.default = simo; - devShells.default = pkgs.mkShell.override { - stdenv = pkgs.clangStdenv; - } { - packages = with pkgs; [ - boost - cmake - ninja - clang-tools - llvm - glaze - doxygen - ast-grep - ]; - }; + formatter = nixpkgs.legacyPackages.${system}.nixfmt; + + devShells.default = + pkgs.mkShell.override + { + stdenv = pkgs.clangStdenv; + } + { + inputsFrom = [ simo ]; + packages = with pkgs; [ + clang-tools + ast-grep + ]; + }; } ); } diff --git a/support/ides/clion/README.md b/support/ides/clion/README.md new file mode 100644 index 0000000..66678d1 --- /dev/null +++ b/support/ides/clion/README.md @@ -0,0 +1,11 @@ +# Use Nix packages in CLion + +Based on [this](https://gist.github.com/pmenke-de/2fed80213c48c2fe80891678f4fa3b42), +but reworked to use flakes. + +1. Run `support/ides/clion/setup-symlinks.sh` to expose some nix binaries +2. In `Settings` -> `Build, Execution, Deployment` -> `Toolchains`, create a new toolchain +3. Set CMake executable to `support/ides/clion/nix-cmake.sh` and the other elements of the +toolchain to the symlinks created in step 1. + +Note it is important to set the symlink of ctest to be able to run unit-tests from CLion. \ No newline at end of file diff --git a/support/ides/clion/nix-cmake.sh b/support/ides/clion/nix-cmake.sh new file mode 100755 index 0000000..d5fcb49 --- /dev/null +++ b/support/ides/clion/nix-cmake.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env sh +# +# Copyright 2026 Matteo Fusi and Contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# 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"} "$@" + + +SCRIPT_DIR=$(cd -- "$(dirname -- "$0")" && pwd) +PROJECT_DIR=$SCRIPT_DIR/../../../ +set -x +nix develop "$PROJECT_DIR" --command "$SCRIPT_DIR/nix-run.sh" cmake "$@" +exit $? + + diff --git a/support/ides/clion/nix-run.sh b/support/ides/clion/nix-run.sh new file mode 100755 index 0000000..2486b78 --- /dev/null +++ b/support/ides/clion/nix-run.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env sh +# +# Copyright 2026 Matteo Fusi and Contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +set -x +"$@" \ No newline at end of file diff --git a/support/ides/clion/setup-symlinks.sh b/support/ides/clion/setup-symlinks.sh new file mode 100755 index 0000000..2d426a7 --- /dev/null +++ b/support/ides/clion/setup-symlinks.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env sh +# +# Copyright 2026 Matteo Fusi and Contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +SCRIPT_DIR=$(cd -- "$(dirname -- "$0")" && pwd) + +# Run bash inside dev shell to create symlinks +nix develop --command bash -c " + ln -sf $(which ninja) $SCRIPT_DIR/ + ln -sf $(which clang) $SCRIPT_DIR/ + ln -sf $(which clang++) $SCRIPT_DIR/ + ln -sf $(which ctest) $SCRIPT_DIR/ +"