diff --git a/apps/default.nix b/apps/default.nix new file mode 100644 index 0000000..d36ec12 --- /dev/null +++ b/apps/default.nix @@ -0,0 +1,14 @@ +{ self, nixpkgs, ... }: +let + forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" ]; + nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; }); + +in forAllSystems(system: let + pkgs = nixpkgsFor.${system}; + bpkgs = self.packages.${system}; + blib = self.lib; + in { + + example = blib.mkApp { drv = bpkgs.example; name = "hello-nix"; }; + +}) diff --git a/flake.nix b/flake.nix index 489bed1..7ad5ef8 100644 --- a/flake.nix +++ b/flake.nix @@ -9,6 +9,9 @@ }; outputs = inputs @ { self, nixpkgs, home-manager, agenix, ... }: { + + lib = import ./lib {}; + nixosConfigurations = { astora = with nixpkgs; lib.nixosSystem { system = "x86_64-linux"; @@ -29,5 +32,9 @@ templates = { rust = { path = ./templates/rust; description = "Basic Rust template"; }; }; + + packages = import ./packages { inherit self nixpkgs; }; + + apps = import ./apps { inherit self nixpkgs; }; }; } diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..50ea6e2 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,8 @@ +{ ... }: +{ + mkApp = { drv, name ? drv.pname, binPath ? "/bin/${name}" }: + { + type = "app"; + program = "${drv}${binPath}"; + }; +} diff --git a/packages/default.nix b/packages/default.nix new file mode 100644 index 0000000..371fc35 --- /dev/null +++ b/packages/default.nix @@ -0,0 +1,10 @@ +{ self, nixpkgs, ... }: +let + forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" ]; + nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; }); + +in forAllSystems(system: let pkgs = nixpkgsFor.${system}; in { + + example = pkgs.callPackage ./example {}; + +}) diff --git a/packages/example/CMakeLists.txt b/packages/example/CMakeLists.txt new file mode 100644 index 0000000..4b92884 --- /dev/null +++ b/packages/example/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.16) + +project(hello-nix LANGUAGES CXX) + +add_executable(hello-nix source/example.cpp) + +install( + TARGETS hello-nix + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT runtime +) diff --git a/packages/example/default.nix b/packages/example/default.nix new file mode 100644 index 0000000..afc3cd0 --- /dev/null +++ b/packages/example/default.nix @@ -0,0 +1,19 @@ +{ stdenv, lib, pkgs, ... }: +stdenv.mkDerivation { + pname = "example"; + version = "1.0"; + + # local source + src = ./.; + + nativeBuildInputs = with pkgs; [ cmake ninja ]; + + meta = with lib; { + homepage = "https://www.example.org/"; + description = "Example with hello nix."; + license = licenses.cc0; + platforms = platforms.linux; + maintainers = []; + broken = false; + }; +} diff --git a/packages/example/flake.lock b/packages/example/flake.lock new file mode 100644 index 0000000..1b0c91e --- /dev/null +++ b/packages/example/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1694422566, + "narHash": "sha256-lHJ+A9esOz9vln/3CJG23FV6Wd2OoOFbDeEs4cMGMqc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3a2786eea085f040a66ecde1bc3ddc7099f6dbeb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/packages/example/flake.old.nix b/packages/example/flake.old.nix new file mode 100644 index 0000000..2e3f818 --- /dev/null +++ b/packages/example/flake.old.nix @@ -0,0 +1,71 @@ +{ + description = "Example with hello nix."; + nixConfig.bash-prompt = "\[nix-develop\]$ "; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs, ... }: + let + systems = [ "x86_64-linux" ]; + forAllSystems = nixpkgs.lib.genAttrs systems; + nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; }); + + in { + packages = forAllSystems (system: { + example = let + pkgs = nixpkgsFor.${system}; + pname = "example"; + version = "1.0"; + + in pkgs.stdenv.mkDerivation { + inherit pname version; + + # local source + src = ./.; + + nativeBuildInputs = with pkgs; [ cmake ninja ]; + + meta = with pkgs.lib; { + homepage = "https://www.example.org/"; + description = "Example with hello nix."; + license = licenses.cc0; + platforms = platforms.linux; + maintainers = []; + broken = false; + }; + }; + + default = self.packages.${system}.example; + }); + + devShells = forAllSystems (system: { + example = let + pkgs = nixpkgsFor.${system}; + example = self.packages.${system}.example; + + in pkgs.mkShellNoCC { + packages = [ + example + ]; + }; + + default = self.devShells.${system}.example; + }); + + apps = forAllSystems (system: { + example = let + pkgs = nixpkgsFor.${system}; + example = self.packages.${system}.example; + + in { + type = "app"; + program = "${example}/bin/hello-nix"; + }; + + default = self.apps.${system}.example; + }); + + }; +} diff --git a/packages/example/source/example.cpp b/packages/example/source/example.cpp new file mode 100644 index 0000000..9ea2e3c --- /dev/null +++ b/packages/example/source/example.cpp @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + std::cout << "Hello Nix!" << std::endl; + + return 0; +}