From aff83a7bf1b3c145d645c604dc1377e87e07ec1c Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Mon, 6 May 2024 23:40:01 +0200 Subject: [PATCH 1/7] feat(c): add hello source --- c/Makefile | 35 +++++++++++++++++++++++++++++++++++ c/compile_flags.txt | 18 ++++++++++++++++++ c/main.c | 16 ++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 c/Makefile create mode 100644 c/compile_flags.txt create mode 100644 c/main.c diff --git a/c/Makefile b/c/Makefile new file mode 100644 index 0000000..a7f3da0 --- /dev/null +++ b/c/Makefile @@ -0,0 +1,35 @@ +.POSIX: +.SUFFIXES: .o + +CC ?= gcc +OUT := hello + +SRC += main.c +OBJ := $(SRC:.c=.o) + +CFLAGS += @compile_flags.txt +CFLAGS += -ffunction-sections -fdata-sections + +LDFLAGS := -fwhole-program -flto +LDFLAGS += -Wl,--gc-sections -s + +RM ?= rm -f + +.DEFAULT_GOAL: all +.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 diff --git a/c/compile_flags.txt b/c/compile_flags.txt new file mode 100644 index 0000000..19adf53 --- /dev/null +++ b/c/compile_flags.txt @@ -0,0 +1,18 @@ +-std=c99 +-pedantic +-pipe + +-Wall +-Wcast-qual +-Wconversion +-Werror=return-type +-Werror=vla-larger-than=0 +-Wextra +-Wmissing-prototypes +-Wshadow +-Wstrict-prototypes +-Wwrite-strings + +-O2 +-march=native +-mtune=native diff --git a/c/main.c b/c/main.c new file mode 100644 index 0000000..30a4d72 --- /dev/null +++ b/c/main.c @@ -0,0 +1,16 @@ +#include +#include + +#define lengthof(sstr) (sizeof (sstr) / sizeof *(sstr)) +#define sstr_len(sstr) (lengthof(sstr) - 1) +#define sstr_unpack(sstr) (sstr), (sstr_len(sstr)) + +static const char GREETING[] = "hello, world!\n"; + +int main(void) +{ + return ( + write(STDOUT_FILENO, sstr_unpack(GREETING)) + == sstr_len(GREETING) + ) ? EXIT_SUCCESS : EXIT_FAILURE; +} From 30022cc91493d00a8dbc92823a65e399031a4d3b Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Mon, 6 May 2024 23:40:26 +0200 Subject: [PATCH 2/7] feat(c): setup the flake --- c/flake.nix | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 c/flake.nix diff --git a/c/flake.nix b/c/flake.nix new file mode 100644 index 0000000..0392f3a --- /dev/null +++ b/c/flake.nix @@ -0,0 +1,48 @@ +{ + description = "Templates for getting started with Aux"; + + inputs.nixpkgs.url = "github:auxolotl/nixpkgs/nixos-unstable"; + + outputs = { self, nixpkgs }: + let + forAllSystems = + function: + nixpkgs.lib.genAttrs [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + "i686-linux" + "mipsel-linux" + "powerpc64le-linux" + ] (system: function nixpkgs.legacyPackages.${system}); + in rec { + devShells.default = forAllSystems (pkgs: + pkgs.mkShell { + hardeningDisable = [ "fortify" ]; + inputsFrom = pkgs.lib.attrsets.attrValues packages; + }); + + packages = forAllSystems (pkgs: rec { + default = hello; + hello = pkgs.stdenv.mkDerivation rec { + name = "hello"; + + src = ./.; + nativeBuildInputs = [ pkgs.gnumake ]; + + enableParallelBuilding = true; + V = 1; + installPhase = '' + install -D ${name} $out/bin/${name} --mode 0755 + ''; + }; + }); + + apps = rec { + default = hello; + hello = builtins.mapAttrs + (name: value: "${value.hello}/bin/hello") packages; + }; + }; +} From c1524d5bd9b8035d51107112c52066bb8ef5ea13 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Mon, 6 May 2024 23:40:38 +0200 Subject: [PATCH 3/7] feat(c): add configuration files --- c/.editorconfig | 22 ++++++++++++++++++++++ c/.gitattributes | 2 ++ c/.gitignore | 15 +++++++++++++++ c/Dockerfile | 29 +++++++++++++++++++++++++++++ c/tokei.toml | 4 ++++ 5 files changed, 72 insertions(+) create mode 100644 c/.editorconfig create mode 100644 c/.gitattributes create mode 100644 c/.gitignore create mode 100644 c/Dockerfile create mode 100644 c/tokei.toml 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/.gitattributes b/c/.gitattributes new file mode 100644 index 0000000..1457a77 --- /dev/null +++ b/c/.gitattributes @@ -0,0 +1,2 @@ +* text=lf +* eol=lf diff --git a/c/.gitignore b/c/.gitignore new file mode 100644 index 0000000..fc5e2c9 --- /dev/null +++ b/c/.gitignore @@ -0,0 +1,15 @@ +# binaries +hello + +# build +*.[aod] + +# config +compile_commands.json +.pre-commit-config.yaml +.cache + +# nix +.direnv +.envrc +result diff --git a/c/Dockerfile b/c/Dockerfile new file mode 100644 index 0000000..4551ac5 --- /dev/null +++ b/c/Dockerfile @@ -0,0 +1,29 @@ +# Credit to Mitchell Hashimoto +# post: https://mitchellh.com/writing/nix-with-dockerfiles + +# Nix builder +FROM nixos/nix:latest AS builder + +# Copy our source and setup our working dir. +COPY . /tmp/build +WORKDIR /tmp/build + +# Build our Nix environment +RUN nix \ + --extra-experimental-features "nix-command flakes" \ + --option filter-syscalls false \ + build + +# Copy the Nix store closure into a directory. The Nix store closure is the +# entire set of Nix store values that we need for our build. +RUN mkdir /tmp/nix-store-closure +RUN cp -R $(nix-store -qR result/) /tmp/nix-store-closure + +# Final image is based on scratch. We copy a bunch of Nix dependencies +# but they're fully self-contained so we don't need Nix anymore. +FROM scratch + +# Copy /nix/store +COPY --from=builder /tmp/nix-store-closure /nix/store +COPY --from=builder /tmp/build/result /app +CMD ["/app/bin/hello"] diff --git a/c/tokei.toml b/c/tokei.toml new file mode 100644 index 0000000..8f7e17d --- /dev/null +++ b/c/tokei.toml @@ -0,0 +1,4 @@ +columns = 80 +sort = "lines" +types = ["C", "C Header", "Makefile", "Markdown"] +treat_doc_strings_as_comments = true From a95b46a6a0fb4ac672b70b5431b3679069ad1839 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Mon, 6 May 2024 23:46:22 +0200 Subject: [PATCH 4/7] style(c): apply nixfmt-rfc-style to flake --- c/flake.nix | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/c/flake.nix b/c/flake.nix index 0392f3a..2188e03 100644 --- a/c/flake.nix +++ b/c/flake.nix @@ -3,7 +3,8 @@ inputs.nixpkgs.url = "github:auxolotl/nixpkgs/nixos-unstable"; - outputs = { self, nixpkgs }: + outputs = + { self, nixpkgs }: let forAllSystems = function: @@ -16,12 +17,15 @@ "mipsel-linux" "powerpc64le-linux" ] (system: function nixpkgs.legacyPackages.${system}); - in rec { - devShells.default = forAllSystems (pkgs: + in + rec { + devShells.default = forAllSystems ( + pkgs: pkgs.mkShell { hardeningDisable = [ "fortify" ]; inputsFrom = pkgs.lib.attrsets.attrValues packages; - }); + } + ); packages = forAllSystems (pkgs: rec { default = hello; @@ -41,8 +45,7 @@ apps = rec { default = hello; - hello = builtins.mapAttrs - (name: value: "${value.hello}/bin/hello") packages; + hello = builtins.mapAttrs (name: value: "${value.hello}/bin/hello") packages; }; }; } From e2607d77cc06baf959d09f305cd429f164192461 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Mon, 6 May 2024 23:53:12 +0200 Subject: [PATCH 5/7] fix(c): fix flake output (devshell, apps) --- c/flake.nix | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/c/flake.nix b/c/flake.nix index 2188e03..ed72ed7 100644 --- a/c/flake.nix +++ b/c/flake.nix @@ -19,13 +19,12 @@ ] (system: function nixpkgs.legacyPackages.${system}); in rec { - devShells.default = forAllSystems ( - pkgs: - pkgs.mkShell { + devShells = forAllSystems (pkgs: { + default = pkgs.mkShell { hardeningDisable = [ "fortify" ]; inputsFrom = pkgs.lib.attrsets.attrValues packages; - } - ); + }; + }); packages = forAllSystems (pkgs: rec { default = hello; @@ -43,9 +42,12 @@ }; }); - apps = rec { + apps = forAllSystems (pkgs: rec { default = hello; - hello = builtins.mapAttrs (name: value: "${value.hello}/bin/hello") packages; - }; + hello = { + program = "${packages.${pkgs.system}.hello}/bin/hello"; + type = "app"; + }; + }); }; } From 57769ca9edb6e74d483017d7581bc4f0874a1272 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Tue, 7 May 2024 00:21:40 +0200 Subject: [PATCH 6/7] refactor(c): improve flake structure Co-authored-by: isabelroses --- c/default.nix | 13 +++++++++++++ c/flake.nix | 17 ++++------------- 2 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 c/default.nix diff --git a/c/default.nix b/c/default.nix new file mode 100644 index 0000000..444469b --- /dev/null +++ b/c/default.nix @@ -0,0 +1,13 @@ +{ stdenv, gnumake }: +stdenv.mkDerivation { + name = "hello"; + + src = ./.; + nativeBuildInputs = [ gnumake ]; + + enableParallelBuilding = true; + + installPhase = '' + install -D hello $out/bin/hello --mode 0755 + ''; +} diff --git a/c/flake.nix b/c/flake.nix index ed72ed7..d1fa496 100644 --- a/c/flake.nix +++ b/c/flake.nix @@ -1,5 +1,5 @@ { - description = "Templates for getting started with Aux"; + description = "Aux template for C project"; inputs.nixpkgs.url = "github:auxolotl/nixpkgs/nixos-unstable"; @@ -28,20 +28,11 @@ packages = forAllSystems (pkgs: rec { default = hello; - hello = pkgs.stdenv.mkDerivation rec { - name = "hello"; - - src = ./.; - nativeBuildInputs = [ pkgs.gnumake ]; - - enableParallelBuilding = true; - V = 1; - installPhase = '' - install -D ${name} $out/bin/${name} --mode 0755 - ''; - }; + hello = pkgs.callPackage ./default.nix { }; }); + overlays.default = final: prev: { hello = final.callPackage ./default.nix { }; }; + apps = forAllSystems (pkgs: rec { default = hello; hello = { From d981bc409ecd1da0eaa0908165e452438b60cee1 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Tue, 7 May 2024 09:08:20 +0200 Subject: [PATCH 7/7] fix(c): simplify the template config files --- c/.gitattributes | 2 -- c/.gitignore | 63 ++++++++++++++++++++++++++++++++++++++++----- c/Dockerfile | 29 --------------------- c/Makefile | 19 +++++--------- c/compile_flags.txt | 18 ------------- c/default.nix | 13 ---------- c/flake.nix | 29 +++++---------------- c/hello.nix | 7 +++++ c/main.c | 17 +++++------- c/tokei.toml | 4 --- 10 files changed, 82 insertions(+), 119 deletions(-) delete mode 100644 c/.gitattributes delete mode 100644 c/Dockerfile delete mode 100644 c/compile_flags.txt delete mode 100644 c/default.nix create mode 100644 c/hello.nix delete mode 100644 c/tokei.toml diff --git a/c/.gitattributes b/c/.gitattributes deleted file mode 100644 index 1457a77..0000000 --- a/c/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -* text=lf -* eol=lf diff --git a/c/.gitignore b/c/.gitignore index fc5e2c9..a07179f 100644 --- a/c/.gitignore +++ b/c/.gitignore @@ -1,15 +1,64 @@ # binaries hello -# build -*.[aod] - -# config +# language support compile_commands.json -.pre-commit-config.yaml .cache # nix .direnv -.envrc -result +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/Dockerfile b/c/Dockerfile deleted file mode 100644 index 4551ac5..0000000 --- a/c/Dockerfile +++ /dev/null @@ -1,29 +0,0 @@ -# Credit to Mitchell Hashimoto -# post: https://mitchellh.com/writing/nix-with-dockerfiles - -# Nix builder -FROM nixos/nix:latest AS builder - -# Copy our source and setup our working dir. -COPY . /tmp/build -WORKDIR /tmp/build - -# Build our Nix environment -RUN nix \ - --extra-experimental-features "nix-command flakes" \ - --option filter-syscalls false \ - build - -# Copy the Nix store closure into a directory. The Nix store closure is the -# entire set of Nix store values that we need for our build. -RUN mkdir /tmp/nix-store-closure -RUN cp -R $(nix-store -qR result/) /tmp/nix-store-closure - -# Final image is based on scratch. We copy a bunch of Nix dependencies -# but they're fully self-contained so we don't need Nix anymore. -FROM scratch - -# Copy /nix/store -COPY --from=builder /tmp/nix-store-closure /nix/store -COPY --from=builder /tmp/build/result /app -CMD ["/app/bin/hello"] diff --git a/c/Makefile b/c/Makefile index a7f3da0..a4d834c 100644 --- a/c/Makefile +++ b/c/Makefile @@ -1,21 +1,12 @@ -.POSIX: -.SUFFIXES: .o - CC ?= gcc +CFLAGS += -pedantic -Wall -Wextra -O2 + OUT := hello +BINDIR ?= /usr/bin SRC += main.c OBJ := $(SRC:.c=.o) -CFLAGS += @compile_flags.txt -CFLAGS += -ffunction-sections -fdata-sections - -LDFLAGS := -fwhole-program -flto -LDFLAGS += -Wl,--gc-sections -s - -RM ?= rm -f - -.DEFAULT_GOAL: all .PHONY: all all: $(OUT) @@ -33,3 +24,7 @@ fclean: clean .PHONY: re .NOTPARALLEL: re re: fclean all + +.PHONY: install +install: + install -D hello ${BINDIR}/hello --mode 0755 diff --git a/c/compile_flags.txt b/c/compile_flags.txt deleted file mode 100644 index 19adf53..0000000 --- a/c/compile_flags.txt +++ /dev/null @@ -1,18 +0,0 @@ --std=c99 --pedantic --pipe - --Wall --Wcast-qual --Wconversion --Werror=return-type --Werror=vla-larger-than=0 --Wextra --Wmissing-prototypes --Wshadow --Wstrict-prototypes --Wwrite-strings - --O2 --march=native --mtune=native diff --git a/c/default.nix b/c/default.nix deleted file mode 100644 index 444469b..0000000 --- a/c/default.nix +++ /dev/null @@ -1,13 +0,0 @@ -{ stdenv, gnumake }: -stdenv.mkDerivation { - name = "hello"; - - src = ./.; - nativeBuildInputs = [ gnumake ]; - - enableParallelBuilding = true; - - installPhase = '' - install -D hello $out/bin/hello --mode 0755 - ''; -} diff --git a/c/flake.nix b/c/flake.nix index d1fa496..4798ff9 100644 --- a/c/flake.nix +++ b/c/flake.nix @@ -8,37 +8,20 @@ let forAllSystems = function: - nixpkgs.lib.genAttrs [ - "x86_64-linux" - "aarch64-linux" - "x86_64-darwin" - "aarch64-darwin" - "i686-linux" - "mipsel-linux" - "powerpc64le-linux" - ] (system: function nixpkgs.legacyPackages.${system}); + nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed ( + system: function nixpkgs.legacyPackages.${system} + ); in rec { devShells = forAllSystems (pkgs: { - default = pkgs.mkShell { - hardeningDisable = [ "fortify" ]; - inputsFrom = pkgs.lib.attrsets.attrValues packages; - }; + default = pkgs.mkShell { inputsFrom = [ packages.${pkgs.system}.hello ]; }; }); packages = forAllSystems (pkgs: rec { default = hello; - hello = pkgs.callPackage ./default.nix { }; + hello = pkgs.callPackage ./hello.nix { }; }); - overlays.default = final: prev: { hello = final.callPackage ./default.nix { }; }; - - apps = forAllSystems (pkgs: rec { - default = hello; - hello = { - program = "${packages.${pkgs.system}.hello}/bin/hello"; - type = "app"; - }; - }); + 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 index 30a4d72..9d42f19 100644 --- a/c/main.c +++ b/c/main.c @@ -1,16 +1,11 @@ +#include #include -#include - -#define lengthof(sstr) (sizeof (sstr) / sizeof *(sstr)) -#define sstr_len(sstr) (lengthof(sstr) - 1) -#define sstr_unpack(sstr) (sstr), (sstr_len(sstr)) - -static const char GREETING[] = "hello, world!\n"; int main(void) { - return ( - write(STDOUT_FILENO, sstr_unpack(GREETING)) - == sstr_len(GREETING) - ) ? EXIT_SUCCESS : EXIT_FAILURE; + char greet[] = "hello, world!\n"; + int written = printf("%s", greet); + + return written == (sizeof(greet) - 1) + ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/c/tokei.toml b/c/tokei.toml deleted file mode 100644 index 8f7e17d..0000000 --- a/c/tokei.toml +++ /dev/null @@ -1,4 +0,0 @@ -columns = 80 -sort = "lines" -types = ["C", "C Header", "Makefile", "Markdown"] -treat_doc_strings_as_comments = true