nkoturovic / cpp-nix-project-template

Starter project for C++ application with nix as a package manager

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Template iso-cpp Built with nix kotur.me

C++ & Nix project template

Starter project for your C++ application with nix as a package manager
If you don't know what nix package manager is, please look at the FAQ section

About project

Configured to work when used both as a regular nix derivation, and as a flake. In both cases, the version of nixpkgs is used from flake.lock (JSON file). That way, we get some of the flake advantages even when using it as a regular derivation.

Similar to nixpkgs, project leverages the custom kotur-nixpkgs channel for all package requirements that are not available within nixpkgs. One such example is the python package dinosay, which is being used to display the welcome message at the moment of entering dev shell with nix-shell command.

Project structure

  • default.nix - Definition for the package being defined by this repo, list of dependencies (nix packages)
  • shell.nix - Uses default.nix to read shell definition and exposes it to the user
  • flake.nix - Enables using the package as a flake
  • flake.lock - Locked version of packages (mainly nixpkgs) which are used both for default flake use-case

Commands

In this section, you can find various commands that can help you use the full potential of tools used within the project

Build with Nix

  • Building the package
    • nix-build - default way
    • nix build - flakes way

Develop with Nix

  • Enter the development environment (shell)
    • nix-shell - default way, use --pure to enter shell in pure mode
    • nix develop - flakes way, use -i to ignore the environment

Other nix commands

  • nix flake update - Updates flake.lock file (used both by flake.nix and default.nix)
  • nix fmt - Format nix files based on a formatter specified in flake.nix file

Flakes are still an experimental feature of Nix, to add flake support look at the flakes documentation

Building with CMake from dev shell

After entering the dev environment, you can use the standard CMake procedure to build the project.

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug
make -j$(nproc)

Exporting compile_commands.json

In order to have autocompletion for the project, you can leverage compile_commands.json which can be exported by following the steps described bellow

  1. Open two terminal/shell instances and cd into the project

  2. In both terminals enter the development shell (look how in Nix commands section)

  3. In terminal one type

    mini_compile_commands_server.py
  4. In terminal two use the standard CMake build procedure without the final (gnu) make command

  5. In terminal one, terminate the app with SIGINT by pressing Ctrl+c

  6. compile_commands.json file should be generated at the root of the project

  7. Close terminal one, continue using or close terminal two

FAQ

List of frequiently asked questions:

About

Starter project for C++ application with nix as a package manager

License:MIT License


Languages

Language:Nix 93.8%Language:CMake 4.3%Language:C++ 1.8%