Skip to content

Commit

Permalink
templates: init
Browse files Browse the repository at this point in the history
Copy a bunch of templates over. Would like to take advantage of this
more.
  • Loading branch information
khaneliman committed May 24, 2024
1 parent ad0e40f commit 161ec53
Show file tree
Hide file tree
Showing 54 changed files with 3,318 additions and 0 deletions.
13 changes: 13 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@
};
};

templates = {
c.description = "C flake template.";
container.description = "Container template";
cpp.description = "CPP flake template";
dotnetf.description = "Dotnet FSharp template";
flake-compat.description = "Flake-compat shell and default files.";
go.description = "Go template";
node.description = "Node template";
python.description = "Python template";
rust.description = "Rust template";
rust-web-server.description = "Rust web server template";
};

deploy = lib.mkDeploy { inherit (inputs) self; };

outputs-builder = channels: { formatter = channels.nixpkgs.nixfmt-rfc-style; };
Expand Down
13 changes: 13 additions & 0 deletions templates/c/Makefile.in
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions templates/c/configure.ac
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AC_INIT([Hello], 1.0)
AC_PROG_CC
AC_CONFIG_FILES(Makefile)
AC_OUTPUT
125 changes: 125 additions & 0 deletions templates/c/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
description = "An over-engineered Hello World in C";

# Nixpkgs / NixOS version to use.
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.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")
'';
};
}
);
};
}
3 changes: 3 additions & 0 deletions templates/c/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "stdio.h"

int main(int argc, char **argv) { printf("Hello Nixers!\n"); }
31 changes: 31 additions & 0 deletions templates/container/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";

outputs =
{ self, nixpkgs }:
{

nixosConfigurations.container = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
(_: {
boot.isContainer = true;

# Let 'nixos-version --json' know about the Git revision
# of this flake.
system.configurationRevision = nixpkgs.lib.mkIf (self ? rev) self.rev;

# Network configuration.
networking.useDHCP = false;
networking.firewall.allowedTCPPorts = [ 80 ];

# Enable a web server.
services.httpd = {
enable = true;
adminAddr = "[email protected]";
};
})
];
};
};
}
15 changes: 15 additions & 0 deletions templates/cpp/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.c]
ident_style = space
ident_size = 4

[Makefile*]
ident_style = tab
ident_size = 4
3 changes: 3 additions & 0 deletions templates/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ignore build artifacts
result
build
42 changes: 42 additions & 0 deletions templates/cpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
PREFIX ?= /usr/local # this is overridden by the derivation makeFlags
BIN_DIR ?= $(PREFIX)/bin


TARGET_EXEC ?= foo-bar
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src

SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)

INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))

CPPFLAGS ?= $(INC_FLAGS) -MMD -MP

$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LDFLAGS)

# c source
$(BUILD_DIR)/%.c.o: %.c
mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
mkdir -p $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

.PHONY: clean install run

clean:
rm -r $(BUILD_DIR)

install: $(BUILD_DIR)/$(TARGET_EXEC)
install -Dt $(BIN_DIR) $<

run: $(BUILD_DIR)/$(TARGET_EXEC)
./$<

-include $(DEPS)
9 changes: 9 additions & 0 deletions templates/cpp/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ clangStdenv }:
clangStdenv.mkDerivation {
pname = "sample-c-cpp";
version = "0.0.1";

src = ./.;

makeFlags = [ "PREFIX=$(out)" ];
}
27 changes: 27 additions & 0 deletions templates/cpp/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
description = "C/C++ Project Template";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
};

outputs =
{ nixpkgs, ... }:
let
systems = [
"x86_64-linux"
"aarch64-linux"
];
forEachSystem = nixpkgs.lib.genAttrs systems;

pkgsForEach = nixpkgs.legacyPackages;
in
{
packages = forEachSystem (system: {
default = pkgsForEach.${system}.callPackage ./default.nix { };
});

devShells = forEachSystem (system: {
default = pkgsForEach.${system}.callPackage ./shell.nix { };
});
};
}
35 changes: 35 additions & 0 deletions templates/cpp/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
callPackage,
clang-tools,
gnumake,
cmake,
bear,
libcxx,
cppcheck,
llvm,
gdb,
glm,
SDL2,
SDL2_gfx,
}:
let
mainPkg = callPackage ./default.nix { };
in
mainPkg.overrideAttrs (oa: {
nativeBuildInputs = [
clang-tools # fix headers not found
gnumake # builder
cmake # another builder
bear # bear.
libcxx # stdlib for cpp
cppcheck # static analysis
llvm.lldb # debugger
gdb # another debugger
llvm.libstdcxxClang # LSP and compiler
llvm.libcxx # stdlib for C++
# libs
glm
SDL2
SDL2_gfx
] ++ (oa.nativeBuildInputs or [ ]);
})
7 changes: 7 additions & 0 deletions templates/cpp/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>

int main() {
std::cout << "Hello, World!";

return 0;
}
37 changes: 37 additions & 0 deletions templates/dotnetf/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
root=true

[*]
charset=utf-8
trim_trailing_whitespace=true
insert_final_newline=true
indent_style=space
indent_size=4

# ReSharper properties
resharper_xml_indent_size=2
resharper_xml_max_line_length=100
resharper_xml_tab_width=2

[*.{csproj,fsproj,sqlproj,targets,props,ts,tsx,css,json}]
indent_size=2

[*.{fs,fsi}]
fsharp_bar_before_discriminated_union_declaration=true
fsharp_space_before_uppercase_invocation=true
fsharp_space_before_class_constructor=true
fsharp_space_before_member=true
fsharp_space_before_colon=true
fsharp_space_before_semicolon=true
fsharp_multiline_block_brackets_on_same_column=true
fsharp_newline_between_type_definition_and_members=true
fsharp_align_function_signature_to_indentation=true
fsharp_alternative_long_member_definitions=true
fsharp_multi_line_lambda_closing_newline=true
fsharp_experimental_keep_indent_in_branch=true
fsharp_max_value_binding_width=80
fsharp_max_record_width=0
max_line_length=120

[*.{appxmanifest,build,dtd,nuspec,xaml,xamlx,xoml,xsd}]
indent_size=2
tab_width=2
3 changes: 3 additions & 0 deletions templates/dotnetf/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* eol=auto
*.sh eol=lf
*.nix eol=lf
5 changes: 5 additions & 0 deletions templates/dotnetf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin/
obj/
result
*.DotSettings.user
.idea/
Loading

0 comments on commit 161ec53

Please sign in to comment.