diff --git a/c/.editorconfig b/c/.editorconfig new file mode 100644 index 0000000..318b9fc --- /dev/null +++ b/c/.editorconfig @@ -0,0 +1,22 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +max_line_length = 80 +tab_width = 4 + +[{Makefile,*.mk}] +indent_style = tab + +[*.nix] +indent_style = space +tab_width = 2 +indent_size = 2 + +[*.lock] +indent_style = unset +insert_final_newline = unset diff --git a/c/.gitignore b/c/.gitignore new file mode 100644 index 0000000..a07179f --- /dev/null +++ b/c/.gitignore @@ -0,0 +1,64 @@ +# binaries +hello + +# language support +compile_commands.json +.cache + +# nix +.direnv +result* +repl-result-* + +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/c/Makefile b/c/Makefile new file mode 100644 index 0000000..a4d834c --- /dev/null +++ b/c/Makefile @@ -0,0 +1,30 @@ +CC ?= gcc +CFLAGS += -pedantic -Wall -Wextra -O2 + +OUT := hello +BINDIR ?= /usr/bin + +SRC += main.c +OBJ := $(SRC:.c=.o) + +.PHONY: all +all: $(OUT) + +$(OUT): $(OBJ) + $(CC) -o $@ $< + +.PHONY: clean +clean: + $(RM) $(OBJ) + +.PHONY: fclean +fclean: clean + $(RM) -r $(OUT) + +.PHONY: re +.NOTPARALLEL: re +re: fclean all + +.PHONY: install +install: + install -D hello ${BINDIR}/hello --mode 0755 diff --git a/c/flake.nix b/c/flake.nix new file mode 100644 index 0000000..4798ff9 --- /dev/null +++ b/c/flake.nix @@ -0,0 +1,27 @@ +{ + description = "Aux template for C project"; + + inputs.nixpkgs.url = "github:auxolotl/nixpkgs/nixos-unstable"; + + outputs = + { self, nixpkgs }: + let + forAllSystems = + function: + nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed ( + system: function nixpkgs.legacyPackages.${system} + ); + in + rec { + devShells = forAllSystems (pkgs: { + default = pkgs.mkShell { inputsFrom = [ packages.${pkgs.system}.hello ]; }; + }); + + packages = forAllSystems (pkgs: rec { + default = hello; + hello = pkgs.callPackage ./hello.nix { }; + }); + + overlays.default = final: prev: { hello = prev.callPackage ./default.nix { }; }; + }; +} diff --git a/c/hello.nix b/c/hello.nix new file mode 100644 index 0000000..9b81015 --- /dev/null +++ b/c/hello.nix @@ -0,0 +1,7 @@ +{ stdenv }: +stdenv.mkDerivation { + name = "hello"; + src = ./.; + + env.BINDIR = "${placeholder "out"}/bin"; +} diff --git a/c/main.c b/c/main.c new file mode 100644 index 0000000..9d42f19 --- /dev/null +++ b/c/main.c @@ -0,0 +1,11 @@ +#include +#include + +int main(void) +{ + char greet[] = "hello, world!\n"; + int written = printf("%s", greet); + + return written == (sizeof(greet) - 1) + ? EXIT_SUCCESS : EXIT_FAILURE; +}