Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exemple project

Only one of default.nix or build.yml is required

main.c

#include <stdio.h>
#include <stdlib.h>

int main() {

  #if defined __x86_64__
    int result = system("cowsay 'Hello world from x86-64'");
  #elif defined __aarch64__
    int result = system("cowsay 'Hello world from aarch64'");
  #else
    int result = system("cowsay 'Hello world from a weird architecture'");
  #endif

  if (result == -1) {
    fprintf(stderr, "Error: Failed to execute cowsay\n");
    return 1;
  }
}

default.nix

{ pkgs ? import <nixpkgs> {} }:

pkgs.stdenv.mkDerivation {
  pname = "exemple-c";
  version = "0.0.1";

  src = ./.;
  nativeBuildInputs = [ pkgs.makeWrapper ];
  buildInputs = [ pkgs.cowsay ];


  buildPhase = ''
    gcc -o exemple-c main.c
  '';

  postFixup = ''
    wrapProgram "$out/bin/exemple-c" \
      --prefix PATH : ${ pkgs.lib.makeBinPath [ pkgs.cowsay ] }
  '';

  doCheck = true;
  checkPhase = ''
    ./exemple-c 
  '';

  installPhase = ''
    mkdir -p $out/bin/
    cp exemple-c $out/bin/
  '';

}

build.yml

---
- name: Ensure GCC is installed
  ansible.builtin.package:
    name: gcc
    state: present
  become: true

- name: Ensure cowsay is installed
  ansible.builtin.package:
    name: cowsay
    state: present
  become: true

- name: Compile the C program (gcc -o exemple-c main.c)
  ansible.builtin.command:
    cmd: gcc -o exemple-c main.c
    chdir: /tmp/rbuild/test-project/
  register: compile_result

- name: Run compiled program (./exemple-c)
  ansible.builtin.command:
    cmd: ./exemple-c
    chdir: /tmp/rbuild/test-project/
  register: check_result
  
- name: Display check result
  ansible.builtin.debug:
    msg: "Test output (checkPhase): {{ check_result.stdout }}"

- name: Clean up temporary build directory
  ansible.builtin.file:
    path: /tmp/rbuild/test-project
    state: absent