From 85c29f0e639baa2db39b83775143d39c6291861b Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sat, 17 Feb 2024 14:26:33 +0800 Subject: [PATCH] templates/basic-nix-cpp-app: init --- configs/flake-parts/templates.nix | 4 +++ templates/basic-nix-cpp-app/default.nix | 37 +++++++++++++++++++++ templates/basic-nix-cpp-app/meson.build | 11 ++++++ templates/basic-nix-cpp-app/shell.nix | 13 ++++++++ templates/basic-nix-cpp-app/src/main.cc | 29 ++++++++++++++++ templates/basic-nix-cpp-app/src/meson.build | 19 +++++++++++ 6 files changed, 113 insertions(+) create mode 100644 templates/basic-nix-cpp-app/default.nix create mode 100644 templates/basic-nix-cpp-app/meson.build create mode 100644 templates/basic-nix-cpp-app/shell.nix create mode 100644 templates/basic-nix-cpp-app/src/main.cc create mode 100644 templates/basic-nix-cpp-app/src/meson.build diff --git a/configs/flake-parts/templates.nix b/configs/flake-parts/templates.nix index c8c40fea..2baa8c7d 100644 --- a/configs/flake-parts/templates.nix +++ b/configs/flake-parts/templates.nix @@ -6,6 +6,10 @@ path = ../../templates/basic-devshell; description = "Basic development shell template"; }; + basic-nix-cpp-app = { + path = ../../templates/basic-nix-cpp-app; + description = "Basic Nix program with C++ API"; + }; basic-nix-module-flake = { path = ../../templates/basic-nix-module-flake; description = "Basic Nix module flake template"; diff --git a/templates/basic-nix-cpp-app/default.nix b/templates/basic-nix-cpp-app/default.nix new file mode 100644 index 00000000..57f6cd28 --- /dev/null +++ b/templates/basic-nix-cpp-app/default.nix @@ -0,0 +1,37 @@ +{ stdenv +, lib +, cmake +, meson +, ninja +, pkg-config +, boost +, nix +, semver-cpp +}: + +stdenv.mkDerivation { + pname = "app"; + version = "0.1.0"; + + src = ./.; + + nativeBuildInputs = [ + meson + ninja + pkg-config + ]; + + buildInputs = [ + cmake + boost + nix + semver-cpp + ]; + + meta = with lib; { + description = "Basic Nix CLI"; + license = licenses.mit; + maintainers = with maintainers; [ foo-dogsquared ]; + platforms = nix.meta.platforms; + }; +} diff --git a/templates/basic-nix-cpp-app/meson.build b/templates/basic-nix-cpp-app/meson.build new file mode 100644 index 00000000..1eacdf59 --- /dev/null +++ b/templates/basic-nix-cpp-app/meson.build @@ -0,0 +1,11 @@ +project('app', + 'cpp', + version: '0.1.0', + license: 'MIT', + meson_version: '>=0.56.0', + default_options: [ + 'cpp_std=c++20' + ] +) + +subdir('src') diff --git a/templates/basic-nix-cpp-app/shell.nix b/templates/basic-nix-cpp-app/shell.nix new file mode 100644 index 00000000..0bd9b241 --- /dev/null +++ b/templates/basic-nix-cpp-app/shell.nix @@ -0,0 +1,13 @@ +{ pkgs ? import { } }: + +let + app = pkgs.callPackage ./. { }; +in +pkgs.mkShell { + inputsFrom = [ app ]; + + packages = with pkgs; [ + git + clang-tools + ]; +} diff --git a/templates/basic-nix-cpp-app/src/main.cc b/templates/basic-nix-cpp-app/src/main.cc new file mode 100644 index 00000000..6c7f79ab --- /dev/null +++ b/templates/basic-nix-cpp-app/src/main.cc @@ -0,0 +1,29 @@ +// A nifty little app for resolving flakerefs to the locked input. Because the +// C++ API is mostly unstable, this is confirmed to be working with Nix v2.18.2 +// just for reference. + +#include + +#include + +#include +#include +#include + +int main(int argc, char *argv[]) { + nix::initNix(); + + auto store = nix::openStore(); + + try { + nix::FlakeRef originalRef = + nix::parseFlakeRef(argv[1], std::nullopt, true, false); + auto [tree, lockedInput] = originalRef.input.fetch(store); + std::cout << lockedInput.to_string() << std::endl; + } catch (std::exception &e) { + std::cerr << e.what() << std::endl; + return EXIT_SUCCESS; + } + + return EXIT_SUCCESS; +} diff --git a/templates/basic-nix-cpp-app/src/meson.build b/templates/basic-nix-cpp-app/src/meson.build new file mode 100644 index 00000000..58592799 --- /dev/null +++ b/templates/basic-nix-cpp-app/src/meson.build @@ -0,0 +1,19 @@ +app_srcs = [ + 'main.cc', + ] + +NIX_VERSION = '>=2.17' + +deps = [ + dependency('nix-cmd', version: NIX_VERSION), + dependency('nix-main', version: NIX_VERSION), + dependency('nix-expr', version: NIX_VERSION), + dependency('nix-store', version: NIX_VERSION), + ] + +executable('app', + app_srcs, + dependencies: deps, + install: true, + link_args: '-Wl,-lnixfetchers' + )