Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a Nix Flake for m8c #160

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/nix-package.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Update Version and Hash in default.nix
name: Update Version and Hash in flake.nix

on:
push:
Expand Down Expand Up @@ -42,15 +42,15 @@ jobs:
echo "valid=false" >> $GITHUB_OUTPUT
fi

- name: Update version in default.nix
- name: Update version in flake.nix
if: steps.valid-tag.outputs.valid
run: |
latest_annotated_tag="${{ steps.newest-tag.outputs.latest_annotated_tag }}"
latest_annotated_tag_without_v="${latest_annotated_tag#v}" # Remove 'v' prefix
sed -i "s/version = \".*\";/version = \"$latest_annotated_tag_without_v\";/" default.nix
sed -i "s/version = \".*\";/version = \"$latest_annotated_tag_without_v\";/" flake.nix
new_hash=$(nix-prefetch-url --unpack --type sha256 "https://github.com/laamaa/m8c/archive/v$latest_annotated_tag_without_v.tar.gz") # Use updated variable name
sed -i "s/hash = \".*\";/hash = \"sha256:$new_hash\";/" default.nix
sed -i "s/hash = \".*\";/hash = \"sha256:$new_hash\";/" flake.nix
git config user.email "[email protected]"
git config user.name "GitHub Actions"
git commit -am "Update version and hash in default.nix to $latest_annotated_tag_without_v"
git commit -am "Update version and hash in flake.nix to $latest_annotated_tag_without_v"
git push origin HEAD:main
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ sudo dnf install m8c
nix-env -iA m8c-stable -f https://github.com/laamaa/m8c/archive/refs/heads/main.tar.gz
```

Or if you're using flakes and the nix command, you can run the app directly with:

```sh
nix run github:laamaa/m8c
```

### Building from source code

#### Install dependencies
Expand Down
63 changes: 17 additions & 46 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
{ pkgs ? import <nixpkgs> {} }:

with pkgs;

# HOWTO keep this package definition up-to-date:
#
# 1. NEW VERSION:
# After the new version is tagged and pushed, update the `version` var to the
# proper value and update the hash. You can use the following command to find
# out the sha256, for example, with version 1.0.3:
# `nix-prefetch-github --rev v1.0.3 laamaa m8c`
#
# 2. NEW DEPENDENCIES:
# Make sure new dependencies are added. Runtime deps go to buildInputs and
# compile-time deps go to nativeBuildInputs. Use the nixpkgs manual for help
#
let m8c-package =
{ stdenv
, gnumake
, pkg-config
, SDL2
, libserialport
, fetchFromGitHub
}:

let
pname = "m8c";
version = "1.7.6";
in
stdenv.mkDerivation {
inherit pname version;

src = fetchFromGitHub {
owner = "laamaa";
repo = pname;
rev = "v${version}";
hash = "sha256:1b2wg8zmxisn1kc3wkfzcij1707pz560yq8v6rwjirr0dd27a3n2";
};

installFlags = [ "PREFIX=$(out)" ];
nativeBuildInputs = [ gnumake pkg-config ];
buildInputs = [ SDL2 libserialport ];
};
in {
m8c-stable = pkgs.callPackage m8c-package {};
m8c-dev = (pkgs.callPackage m8c-package {}).overrideAttrs (oldAttrs: {src = ./.;});
let
pkgs = (import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{
src = ./.;
}).defaultNix.packages.${builtins.currentSystem};
in
{
inherit (pkgs) m8c-stable m8c-dev;
}
81 changes: 81 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 98 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
systems.url = "github:nix-systems/default";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};

outputs = { self, nixpkgs, systems, treefmt-nix, ... }:
let
# HOWTO keep this package definition up-to-date:
#
# 1. NEW VERSION:
# After the new version is tagged and pushed, update the `version` var to the
# proper value and update the hash. You can use the following command to find
# out the sha256, for example, with version 1.0.3:
# `nix-prefetch-github --rev v1.0.3 laamaa m8c`
#
# 2. NEW DEPENDENCIES:
# Make sure new dependencies are added. Runtime deps go to buildInputs and
# compile-time deps go to nativeBuildInputs. Use the nixpkgs manual for help
pname = "m8c";
version = "1.7.6";
m8c-package =
{ stdenv
, gnumake
, pkg-config
, SDL2
, libserialport
, fetchFromGitHub
}:
stdenv.mkDerivation {
inherit pname version;

src = fetchFromGitHub {
owner = "laamaa";
repo = pname;
rev = "v${version}";
hash = "sha256:1b2wg8zmxisn1kc3wkfzcij1707pz560yq8v6rwjirr0dd27a3n2";
};

installFlags = [ "PREFIX=$(out)" ];
nativeBuildInputs = [ gnumake pkg-config ];
buildInputs = [ SDL2 libserialport ];
};
eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f
(import nixpkgs { inherit system; })
);
treefmtEval = eachSystem (pkgs: treefmt-nix.lib.evalModule pkgs (_: {
projectRootFile = "flake.nix";
programs = {
clang-format.enable = false; # TODO(pope): Enable and format C code
deadnix.enable = true;
nixpkgs-fmt.enable = true;
statix.enable = true;
};
}));
in
{
packages = eachSystem (pkgs: rec {
m8c-stable = pkgs.callPackage m8c-package { };
m8c-dev = (pkgs.callPackage m8c-package { }).overrideAttrs (_oldAttrs: {
src = ./.;
});
default = m8c-stable;
});

overlays.default = final: _prev: {
inherit (self.packages.${final.system}) m8c-stable m8c-dev;
};

apps = eachSystem (pkgs: rec {
m8c = {
type = "app";
program = "${self.packages.${pkgs.system}.m8c-stable}/bin/m8c";
};
default = m8c;
});

devShells = eachSystem (pkgs: with pkgs; {
default = mkShell {
packages = [
nix-prefetch-github
treefmtEval.${system}.config.build.wrapper
];
inputsFrom = [ self.packages.${system}.m8c-dev ];
};
});

formatter = eachSystem (pkgs: treefmtEval.${pkgs.system}.config.build.wrapper);
};
}
21 changes: 13 additions & 8 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{ pkgs ? import <nixpkgs> {} }:

with pkgs;

mkShell {
packages = with pkgs; [ nix-prefetch-github ];
inputsFrom = [ (import ./default.nix {}).m8c-dev ];
}
(import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{
src = ./.;
}).shellNix
Loading