nix-c-flake/flake.nix

144 lines
3.4 KiB
Nix
Raw Permalink Normal View History

2024-12-20 16:26:13 +01:00
{
description = "An over-engineered Hello World in C";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
treefmt-nix,
}:
let
# to work with older version of flakes
lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101";
# Generate a user-friendly version number.
version = builtins.substring 0 8 lastModifiedDate;
# System types to support.
supportedSystems = [
"x86_64-linux"
"x86_64-darwin"
"aarch64-linux"
"aarch64-darwin"
];
forEachSupportedSystem =
f:
nixpkgs.lib.genAttrs supportedSystems (
system:
f ({
pkgs = import nixpkgs {
inherit system;
overlays = [ self.overlays.default ];
};
})
);
in
{
formatter = forEachSupportedSystem (
{ pkgs, ... }: (treefmt-nix.lib.evalModule pkgs ./treefmt.nix).config.build.wrapper
);
devShells = forEachSupportedSystem (
{ pkgs, ... }:
{
default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
gcc14
];
};
}
);
packages = forEachSupportedSystem (
{ pkgs, ... }:
{
default = pkgs.hello;
}
);
overlays.default = final: prev: {
hello =
with final;
gcc14Stdenv.mkDerivation {
pname = "main";
inherit version;
src = ./.;
buildPhase = ''
gcc gcc14 --version
gcc --std=gnu23 main.c -o main
'';
installPhase = ''
mkdir -p $out/bin
install ./main $out/bin
'';
};
};
nixosModules.hello =
{ pkgs, ... }:
{
nixpkgs.overlays = [ self.overlay ];
environment.systemPackages = [ pkgs.hello ];
};
# Tests run by 'nix flake check' and by Hydra.
checks = forEachSupportedSystem (
{ pkgs, ... }:
with pkgs;
{
inherit (self.packages.${system}) hello;
# Additional tests, if applicable.
test = stdenv.mkDerivation {
pname = "hello-test";
inherit version;
buildInputs = [ hello ];
dontUnpack = true;
buildPhase = ''
echo 'running some integration tests'
[[ $(hello) = 'Hello Nixers!' ]]
'';
installPhase = "mkdir -p $out";
};
}
// lib.optionalAttrs gcc14stdenv.isLinux {
# A VM test of the NixOS module.
vmTest =
with import (nixpkgs + "/nixos/lib/testing-python.nix") {
inherit system;
};
makeTest {
nodes = {
client =
{ ... }:
{
imports = [ self.nixosModules.hello ];
};
};
testScript = ''
start_all()
client.wait_for_unit("multi-user.target")
client.succeed("hello")
'';
};
}
);
};
}