Skip to content

Commit

Permalink
nix: flake & non-flake dev shells
Browse files Browse the repository at this point in the history
- Added nix stuff
- Documented usage of nix stuff
- Moved #contributing to a separate file
  • Loading branch information
max-ishere committed Oct 20, 2024
1 parent 9ff3e11 commit 71a85e8
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 22 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@
#
# SPDX-License-Identifier: CC0-1.0

# Cargo
target/

# Nix
result

# fakegreet
greetd.sock
2 changes: 1 addition & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Upstream-Name: ReGreet
Upstream-Contact: Harish Rajagopal <harish.rajagopals@gmail.com>
Source: https://github.com/rharish101/ReGreet

Files: Cargo.lock
Files: Cargo.lock flake.lock
Copyright: 2022 Harish Rajagopal <harish.rajagopals@gmail.com>
License: CC0-1.0
57 changes: 57 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!--
SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
SPDX-License-Identifier: GPL-3.0-or-later
-->

# Contributing

## Pre commit

[pre-commit](https://pre-commit.com/) is used for managing hooks that run before each commit (such as clippy), to ensure
code quality. Thus, this needs to be set up only when one intends to commit changes to git.

Firstly, [install pre-commit](https://pre-commit.com/#installation) itself. Next, install pre-commit hooks:
```sh
pre-commit install
```

Now, pre-commit should ensure that the code passes all linters locally before committing. This will save time when
creating PRs, since these linters also run in CI, and thus fail code that hasn't been linted well.

## Nix dev shells

Simply run `nix develop .#vscode --command code .` to get VS Code set up with all the extensions you need. The default
shell doesn't have any text editors configred.

You can also use nix without flakes if you choose to. See the [nix documentation in this repo](nix/README.md) for more
details.

## Testing

You can run ReGreet without a greetd socket using `--demo` flag. It also disables some of the features such as logging
to a file.

```sh
regreet --demo
```

Since the demo mode doesn't use greetd, authentication is done using hardcoded credentials within the codebase. These
credentials are logged with the warning log level, so that you don't have to read the source code.

-----

Alternatively you can use `fakegreet` to emulate a running greetd daemon. Please keep in mind that it's behavior doesn't
match the real thing.

```sh
fakegreet 'cargo run'
```

Fakegreet credentials (taken from source code):

||Value|
|---|---|
|User|user|
|Password|password|
|7+2|9|
23 changes: 2 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,28 +230,9 @@ The recommended configuration is to run greetd greeters as a separate user (`gre
This can lead to insufficient permissions for either creating the cache/log directories, or writing to them.
To make use of the caching and logging features, please create the directories manually with the correct permissions, if not done during installation with systemd-tmpfiles.

## Contributing
[pre-commit](https://pre-commit.com/) is used for managing hooks that run before each commit (such as clippy), to ensure code quality.
Thus, this needs to be set up only when one intends to commit changes to git.
# Contributing

Firstly, [install pre-commit](https://pre-commit.com/#installation) itself.
Next, install pre-commit hooks:
```sh
pre-commit install
```

Now, pre-commit should ensure that the code passes all linters locally before committing.
This will save time when creating PRs, since these linters also run in CI, and thus fail code that hasn't been linted well.
### Demo mode
To aid development, a "demo" mode is included within ReGreet that runs ReGreet independent of greetd.
Simply run ReGreet as follows:
```sh
regreet --demo
```
Since the demo mode doesn't use greetd, authentication is done using hardcoded credentials within the codebase.
These credentials are logged with the warning log-level, so that you don't have to read the source code.
See [CONTRIBUTING.md](CONTRIBUTING.md)

## Licenses
This repository uses [REUSE](https://reuse.software/) to document licenses.
Expand Down
5 changes: 5 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{pkgs ? import <nixpkgs> {}}:
pkgs.callPackage ./nix/packages {}
27 changes: 27 additions & 0 deletions flake.lock

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

41 changes: 41 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{
description = "Dev tooling for ReGreet";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};

outputs = {
self,
nixpkgs,
}: let
supportedSystems = ["x86_64-linux"];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
pkgsFor = system:
import nixpkgs {
config.allowUnfree = true;
inherit system;
};
in {
formatter = forAllSystems (system: (pkgsFor system).alejandra);

packages = forAllSystems (system: let
pkgs = pkgsFor system;
in
{
default = self.packages.${system}.regreet;
}
// (pkgs.callPackage ./nix/packages {}));

devShells = forAllSystems (system: let
pkgs = pkgsFor system;
in
{
default = self.devShells.${system}.rust;
}
// (pkgs.callPackage ./nix/shells {}));
};
}
50 changes: 50 additions & 0 deletions nix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!--
SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
SPDX-License-Identifier: GPL-3.0-or-later
-->

# Nix

## Structure

|Path||
|---|---|
|[`/nix/packages`](packages)|Filenames are keys in both `flake.packages` and attributes in [`/default.nix`](../default.nix)|
|[`/nix/shells`](shells)|Filenames are keys in both `flake.devShells` and attributes in [`/shell.nix`](../shell.nix)|

## Loading a shell / building packages

<details><summary>With flakes</summary>

See the flake source for what `.#default` points to.

```sh
nix develop .#<filename>
nix build .#<filename>

# Example

nix develop .#vscode
# Loads ./shells/vscode.nix
```

</details>

-----

<details><summary>Without flakes</summary>

You have to select an attribute with `-A`. `default` is not set (it doesnt work like that in `nix-*` commnands)!

```sh
nix-shell -A <filename>
nix-build -A <filename>

# Example

nix-build -A regreet
# Builds ./packages/regreet.nix
```

</details>
19 changes: 19 additions & 0 deletions nix/packages/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{
lib,
callPackage,
}: let
inherit (builtins) map listToAttrs filter attrNames readDir;
inherit (lib) removeSuffix;

ls = attrNames (readDir ./.);
notThisFile = name: name != "default.nix";
rmDotNix = removeSuffix ".nix";
mkAttr = file: {
name = rmDotNix file;
value = callPackage ./${file} {};
};
in
listToAttrs (map mkAttr (filter notThisFile ls))
27 changes: 27 additions & 0 deletions nix/packages/regreet.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{
buildFeatures ? ["gtk4_8"],
lib,
rustPlatform,
pkg-config,
wrapGAppsHook4,
glib,
gtk4,
pango,
librsvg,
}: let
manifest = (lib.importTOML ../../Cargo.toml).package;
in
rustPlatform.buildRustPackage rec {
pname = manifest.name;
inherit (manifest) version;
cargoLock.lockFile = ../../Cargo.lock;
src = lib.cleanSource ../..;

inherit buildFeatures;

nativeBuildInputs = [pkg-config wrapGAppsHook4];
buildInputs = [glib gtk4 pango librsvg];
}
19 changes: 19 additions & 0 deletions nix/shells/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{
lib,
callPackage,
}: let
inherit (builtins) map listToAttrs filter attrNames readDir;
inherit (lib) removeSuffix;

ls = attrNames (readDir ./.);
notThisFile = name: name != "default.nix";
rmDotNix = removeSuffix ".nix";
mkAttr = file: {
name = rmDotNix file;
value = callPackage ./${file} {};
};
in
listToAttrs (map mkAttr (filter notThisFile ls))
29 changes: 29 additions & 0 deletions nix/shells/rust.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{
callPackage,
mkShell,
rust-analyzer,
rustfmt,
clippy,
pre-commit,
greetd,
}:
mkShell {
inputsFrom = [(callPackage ../packages/regreet.nix {})];
buildInputs = [
rust-analyzer
rustfmt
clippy

pre-commit

greetd.greetd # fakegreet
];

shellHook = ''
echo "Installing pre commit hooks";
pre-commit install;
'';
}
25 changes: 25 additions & 0 deletions nix/shells/vscode.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{
callPackage,
mkShell,
vscode-with-extensions,
vscode-extensions,
}:
mkShell {
inputsFrom = [(callPackage ./rust.nix {})];
buildInputs = [
(
vscode-with-extensions.override {
vscodeExtensions = with vscode-extensions; [
rust-lang.rust-analyzer
tamasfe.even-better-toml
bbenoist.nix

vscodevim.vim # you can disable this in extension settings if you want
];
}
)
];
}
5 changes: 5 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2024 Harish Rajagopal <harish.rajagopals@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{pkgs ? import <nixpkgs> {config.allowUnfree = true;}}:
pkgs.callPackage ./nix/shells {}

0 comments on commit 71a85e8

Please sign in to comment.