Hi there. I’m just trying to learn more about building programs with nix. I’m trying to build a hello world program with cmake, as I plan to do some more involved development later and I want to get more familiar with the build system.
I have the following directory structure:
package
- basicapp.nix
src
- CMakeLists.txt
- main.cpp
flake.lock
flake.nix
Let’s start with the contents of flake.nix
:
{
description = "Basic App";
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
};
outputs = { ... } @ inputs:
let
pkgs = inputs.nixpkgs.legacyPackages.x86_64-linux;
in rec
{
packages.x86_64-linux.default = pkgs.callPackage ./package/basicapp.nix {};
devShells.x86_64-linux.default = packages.x86_64-linux.default;
};
}
This flake calls a package defined in package/basciapp.nix
{
lib
, stdenv
, cmake
}:
let
fs = lib.fileset;
in
stdenv.mkDerivation {
pname = "basicapp";
version = "1.0";
src = fs.toSource rec {
root = ../src;
fileset = fs.unions [
../src/CMakeLists.txt
(fs.fileFilter (file: file.hasExt "cpp" || file.hasExt "hxx") root)
];
};
nativeBuildInputs = [
cmake
];
}
Here’s CMakeLists.txt:
cmake_minimum_required(VERSION 3.31)
project(bascicapp VERSION 1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(basicapp ./main.cpp)
and, of course, main.cpp which is literally just hello world.
#include <iostream>
int main(int argc, char* argv[]) {
using std::cout, std::endl;
cout << "Hello World!" << endl;
return 0;
}
When I run nix develop
, I can use CMake and build the program and everything is great. I get an executable called basicapp
and can run it just fine. But, when I do nix build
, I get the following error:
error: builder for '/nix/store/d9d5zjhrcd4d9n0szbwxxf175ni0fbgj-basicapp-1.0.drv' failed with exit code 2;
last 25 log lines:
> CMAKE_FIND_USE_PACKAGE_REGISTRY
> CMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY
> CMAKE_INSTALL_BINDIR
> CMAKE_INSTALL_DOCDIR
> CMAKE_INSTALL_INCLUDEDIR
> CMAKE_INSTALL_INFODIR
> CMAKE_INSTALL_LIBDIR
> CMAKE_INSTALL_LIBEXECDIR
> CMAKE_INSTALL_LOCALEDIR
> CMAKE_INSTALL_MANDIR
> CMAKE_INSTALL_SBINDIR
> CMAKE_POLICY_DEFAULT_CMP0025
>
>
> -- Build files have been written to: /build/source/build
> cmake: enabled parallel building
> cmake: enabled parallel installing
> Running phase: buildPhase
> build flags: -j16 SHELL=/nix/store/p79bgyzmmmddi554ckwzbqlavbkw07zh-bash-5.2p37/bin/bash
> [ 50%] Building CXX object CMakeFiles/basicapp.dir/main.cpp.o
> [100%] Linking CXX executable basicapp
> [100%] Built target basicapp
> Running phase: installPhase
> install flags: -j16 SHELL=/nix/store/p79bgyzmmmddi554ckwzbqlavbkw07zh-bash-5.2p37/bin/bash install
> make: *** No rule to make target 'install'. Stop.
For full logs, run:
nix log /nix/store/d9d5zjhrcd4d9n0szbwxxf175ni0fbgj-basicapp-1.0.drv
I’m sure I’m just missing something really simple. I think it’s an install
line in my CMakeLists.txt
, but I don’t know what to put there lol. Any help is appreciated.