Skip to content

Commit

Permalink
chore: migrate to nixfmt rfc style
Browse files Browse the repository at this point in the history
  • Loading branch information
VTimofeenko committed May 14, 2024
1 parent 73c70a5 commit eb47523
Show file tree
Hide file tree
Showing 15 changed files with 410 additions and 193 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
result
*.qcow2
.nixos-test-history
.direnv
33 changes: 17 additions & 16 deletions checks/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{ self, pkgs, lib, ... }:
{
self,
pkgs,
lib,
...
}:
let
checks = [
./xremap-no-features-root-test.nix
Expand All @@ -7,18 +12,14 @@ let
];
inherit (lib) pipe;
in
pipe checks
[
(map (checkFile:
{
name = pipe checkFile [
builtins.toString
builtins.baseNameOf
(lib.replaceStrings [ ".nix" ] [ "" ])
];
value = pkgs.testers.runNixOSTest (import checkFile { inherit self; });
}
))
builtins.listToAttrs
]

pipe checks [
(map (checkFile: {
name = pipe checkFile [
builtins.toString
builtins.baseNameOf
(lib.replaceStrings [ ".nix" ] [ "" ])
];
value = pkgs.testers.runNixOSTest (import checkFile { inherit self; });
}))
builtins.listToAttrs
]
23 changes: 20 additions & 3 deletions checks/xremap-no-features-root-test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,27 @@
services.getty.autologinUser = "root";
imports = [
self.nixosModules.default
{ services.xremap.config.keymap = [{ name = "First remap"; remap = { "9" = "0"; }; }]; }
{ services.xremap.config.keymap = [{ name = "Other remap"; remap = { "z" = "q"; }; }]; }
{
services.xremap.config.keymap = [
{
name = "First remap";
remap = {
"9" = "0";
};
}
];
}
{
services.xremap.config.keymap = [
{
name = "Other remap";
remap = {
"z" = "q";
};
}
];
}
];

};

# This test makes sure that both remaps are applied
Expand Down
111 changes: 61 additions & 50 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
home-manager.url = "github:nix-community/home-manager";
};
outputs =
inputs@{ flake-parts
, self
, ...
}:
inputs@{ flake-parts, self, ... }:
flake-parts.lib.mkFlake { inherit inputs; } (
{ withSystem, flake-parts-lib, ... }:
let
Expand All @@ -51,14 +48,25 @@
"x86_64-linux"
"aarch64-linux"
];
perSystem = { config, self', inputs', pkgs, system, ... }:
perSystem =
{
config,
self',
inputs',
pkgs,
system,
...
}:
let
craneLib = inputs.crane.lib.${system};
inherit (pkgs) lib;
in
{
formatter = pkgs.nixpkgs-fmt;
packages = import ./overlay { inherit (inputs) xremap; inherit craneLib pkgs; };
formatter = pkgs.nixfmt-rfc-style;
packages = import ./overlay {
inherit (inputs) xremap;
inherit craneLib pkgs;
};
# This way all packages get immediately added to the overlay except for the one called literally called "default"
overlayAttrs = builtins.removeAttrs config.packages [ "default" ];
devshells.default = {
Expand All @@ -68,56 +76,53 @@
value = pkgs.rustPlatform.rustLibSrc;
}
];
commands = [
{
help = "Build xremap (no features)";
name = "build-xremap-no-features";
command = "nix build .#";
}
{
help = "Build xremap with features one by one";
name = "test-build-all-features";
command = ''
set -euo pipefail
commands =
[
{
help = "Build xremap (no features)";
name = "build-xremap-no-features";
command = "nix build .#";
}
{
help = "Build xremap with features one by one";
name = "test-build-all-features";
command = ''
set -euo pipefail
features=( "gnome" "hypr" "sway" "x11" "wlroots" "kde" )
features=( "gnome" "hypr" "sway" "x11" "wlroots" "kde" )
for feature in "''${features[@]}"; do
echo "Building feature $feature"
nix build .#xremap-''${feature}
echo "Build successful"
done
'';
}
{
help = "SSH into the dev VM. Disregards the known hosts file";
name = "vm-ssh";
command = ''ssh -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -p 64022 alice@localhost'';
}
]
++
(
# Construct runners for all development VMs
let
definedVMs = self.nixosConfigurations;
namesOfVMs = builtins.attrNames definedVMs;
getVMcomment = vmName:
if builtins.hasAttr "_comment" definedVMs.${vmName}
then definedVMs.${vmName}._comment
else "";
in
map
(nixosSystem: {
for feature in "''${features[@]}"; do
echo "Building feature $feature"
nix build .#xremap-''${feature}
echo "Build successful"
done
'';
}
{
help = "SSH into the dev VM. Disregards the known hosts file";
name = "vm-ssh";
command = ''ssh -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -p 64022 alice@localhost'';
}
]
++ (
# Construct runners for all development VMs
let
definedVMs = self.nixosConfigurations;
namesOfVMs = builtins.attrNames definedVMs;
getVMcomment =
vmName:
if builtins.hasAttr "_comment" definedVMs.${vmName} then definedVMs.${vmName}._comment else "";
in
map (nixosSystem: {
help = "Run VM for testing ${nixosSystem}";
name = "vm-run-${nixosSystem}";
command = ''
echo "Launching VM"
echo "${getVMcomment nixosSystem}"
nix run .#nixosConfigurations.${nixosSystem}.config.system.build.vm
'';
})
namesOfVMs
);
}) namesOfVMs
);
packages = builtins.attrValues {
inherit (pkgs) cargo rustc rustfmt;
inherit (pkgs.rustPackages) clippy;
Expand All @@ -127,8 +132,14 @@
checks = import ./checks { inherit self pkgs lib; };
};
flake = {
nixosModules.default = importApply ./modules { localFlake = self; inherit withSystem; };
homeManagerModules.default = importApply ./homeManagerModules { localFlake = self; inherit withSystem; };
nixosModules.default = importApply ./modules {
localFlake = self;
inherit withSystem;
};
homeManagerModules.default = importApply ./homeManagerModules {
localFlake = self;
inherit withSystem;
};
# nixosConfigurations = import ./nixosConfigurations { localFlake = self; inherit inputs; }; # TODO: restore
localLib = import ./lib { localFlake = self; };
};
Expand Down
7 changes: 6 additions & 1 deletion homeManagerModules/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{ localFlake, withSystem }:
{ config, pkgs, lib, ... }:
{
config,
pkgs,
lib,
...
}:
let
cfg = config.services.xremap;
localLib = localFlake.localLib { inherit pkgs lib cfg; };
Expand Down
42 changes: 33 additions & 9 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{ localFlake }:
{ pkgs, lib, cfg }:
{
pkgs,
lib,
cfg,
}:
let
inherit (pkgs.stdenv.hostPlatform) system;
selfPkgs' = localFlake.packages.${system};
Expand All @@ -25,13 +29,32 @@ in
package = mkOption {
type = types.package;
default =
assert (cfg.withKDE -> (
# TODO: if some other place would need checking that it's a home manager module. If so -- add a "_hm" parameter to the module.
!(builtins.hasAttr "serviceMode" cfg) || (cfg.serviceMode == "user") # First check that "serviceMode" is present in the config. If not -- it's home manager module.
)) || throw "Upstream does not support running withKDE as root";
assert
(
cfg.withKDE
-> (
# TODO: if some other place would need checking that it's a home manager module. If so -- add a "_hm" parameter to the module.
!(builtins.hasAttr "serviceMode" cfg) || (cfg.serviceMode == "user") # First check that "serviceMode" is present in the config. If not -- it's home manager module.
)
)
|| throw "Upstream does not support running withKDE as root";

# Check that 0 or 1 features are enabled, since upstream throws an error otherwise
assert (lib.lists.count (x: x == true) (builtins.attrValues { inherit (cfg) withSway withGnome withX11 withHypr withWlroots withKDE; }) <= 1)
assert
(
lib.lists.count (x: x == true) (
builtins.attrValues {
inherit (cfg)
withSway
withGnome
withX11
withHypr
withWlroots
withKDE
;
}
) <= 1
)
|| throw "Xremap cannot be built with more than one feature. Check that no more than 1 with* feature is enabled";

if cfg.withWlroots then
Expand All @@ -47,8 +70,7 @@ in
else if cfg.withKDE then
selfPkgs'.xremap-kde
else
selfPkgs'.xremap
;
selfPkgs'.xremap;
};
config = mkOption {
type = types.submodule { freeformType = settingsFormat.type; };
Expand Down Expand Up @@ -127,7 +149,9 @@ in
};

configFile =
assert ((cfg.yamlConfig == "" && cfg.config != { }) || (cfg.yamlConfig != "" && cfg.config == { })) || throw "Xremap's config needs to be specified either in .yamlConfig or in .config";
assert
((cfg.yamlConfig == "" && cfg.config != { }) || (cfg.yamlConfig != "" && cfg.config == { }))
|| throw "Xremap's config needs to be specified either in .yamlConfig or in .config";
if cfg.yamlConfig == "" then
settingsFormat.generate "config.yml" cfg.config
else
Expand Down
15 changes: 12 additions & 3 deletions modules/default.nix
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
# localFlake and withSystem allow passing flake to the module through importApply
# See https://flake.parts/define-module-in-separate-file.html
{ localFlake, withSystem }:
{ pkgs, lib, config, ... }:
{
pkgs,
lib,
config,
...
}:
let
cfg = config.services.xremap;
localLib = localFlake.localLib { inherit pkgs lib cfg; };
inherit (localLib) mkExecStart configFile;
in
with lib; {
with lib;
{
imports = [
(import ./user-service.nix { inherit mkExecStart configFile; })
(import ./system-service.nix { inherit mkExecStart configFile; })
];
options.services.xremap = {
serviceMode = mkOption {
type = types.enum [ "user" "system" ];
type = types.enum [
"user"
"system"
];
default = "system";
description = ''
The mode the service will run as.
Expand Down
Loading

0 comments on commit eb47523

Please sign in to comment.