From 0caf5b2378412752a34094a1e89b706956e37e7b Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 12 Sep 2023 10:09:05 -0400 Subject: [PATCH] hello-c from NixOS/templates https://github.com/NixOS/templates/blob/master/c-hello --- Makefile.in | 13 ++++++ configure.ac | 4 ++ flake.nix | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ hello.c | 6 +++ 4 files changed, 139 insertions(+) create mode 100644 Makefile.in create mode 100644 configure.ac create mode 100644 flake.nix create mode 100644 hello.c diff --git a/Makefile.in b/Makefile.in new file mode 100644 index 0000000..f937d5a --- /dev/null +++ b/Makefile.in @@ -0,0 +1,13 @@ +prefix = @prefix@ +exec_prefix = @exec_prefix@ +bindir = @bindir@ + +hello: hello.c + $(CC) -Wall -O3 -o hello hello.c + +clean: + rm -f hello + +install: hello + install -d $(bindir) + install -t $(bindir) hello diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..347364e --- /dev/null +++ b/configure.ac @@ -0,0 +1,4 @@ +AC_INIT([Hello], 1.0) +AC_PROG_CC +AC_CONFIG_FILES(Makefile) +AC_OUTPUT diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2d4bb34 --- /dev/null +++ b/flake.nix @@ -0,0 +1,116 @@ +{ + description = "An over-engineered Hello World in C"; + + # Nixpkgs / NixOS version to use. + inputs.nixpkgs.url = "nixpkgs/nixos-21.05"; + + outputs = { self, nixpkgs }: + 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" ]; + + # Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'. + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + + # Nixpkgs instantiated for supported system types. + nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; overlays = [ self.overlay ]; }); + + in + + { + + # A Nixpkgs overlay. + overlay = final: prev: { + + hello = with final; stdenv.mkDerivation rec { + pname = "hello"; + inherit version; + + src = ./.; + + nativeBuildInputs = [ autoreconfHook ]; + }; + + }; + + # Provide some binary packages for selected system types. + packages = forAllSystems (system: + { + inherit (nixpkgsFor.${system}) hello; + }); + + # The default package for 'nix build'. This makes sense if the + # flake provides only one package or there is a clear "main" + # package. + defaultPackage = forAllSystems (system: self.packages.${system}.hello); + + # A NixOS module, if applicable (e.g. if the package provides a system service). + nixosModules.hello = + { pkgs, ... }: + { + nixpkgs.overlays = [ self.overlay ]; + + environment.systemPackages = [ pkgs.hello ]; + + #systemd.services = { ... }; + }; + + # Tests run by 'nix flake check' and by Hydra. + checks = forAllSystems + (system: + with nixpkgsFor.${system}; + + { + 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 stdenv.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") + ''; + }; + } + ); + + }; +} diff --git a/hello.c b/hello.c new file mode 100644 index 0000000..d67cd5d --- /dev/null +++ b/hello.c @@ -0,0 +1,6 @@ +#include "stdio.h" + +int main(int argc, char * * argv) +{ + printf("Hello Nixers!\n"); +}