python312Packages.dissect-extfs: 3.11 -> 3.12
[NixPkgs.git] / nixos / tests / mpd.nix
blobe268eb573808fc912ab7cf808b145e483ecd20a0
1 import ./make-test-python.nix (
2   { pkgs, lib, ... }:
3   let
4     track = pkgs.fetchurl {
5       # Sourced from http://freemusicarchive.org/music/Blue_Wave_Theory/Surf_Music_Month_Challenge/Skyhawk_Beach_fade_in
7       name = "Blue_Wave_Theory-Skyhawk_Beach.mp3";
8       url = "https://freemusicarchive.org/file/music/ccCommunity/Blue_Wave_Theory/Surf_Music_Month_Challenge/Blue_Wave_Theory_-_04_-_Skyhawk_Beach.mp3";
9       hash = "sha256-91VDWwrcP6Cw4rk72VHvZ8RGfRBrpRE8xo/02dcJhHc=";
10       meta.license = lib.licenses.cc-by-sa-40;
11     };
13     defaultCfg = rec {
14       user = "mpd";
15       group = "mpd";
16       dataDir = "/var/lib/mpd";
17       musicDirectory = "${dataDir}/music";
18     };
20     defaultMpdCfg = {
21       inherit (defaultCfg)
22         dataDir
23         musicDirectory
24         user
25         group
26         ;
27       enable = true;
28     };
30     musicService =
31       {
32         user,
33         group,
34         musicDirectory,
35       }:
36       {
37         description = "Sets up the music file(s) for MPD to use.";
38         requires = [ "mpd.service" ];
39         after = [ "mpd.service" ];
40         wantedBy = [ "default.target" ];
41         script = ''
42           cp ${track} ${musicDirectory}
43         '';
44         serviceConfig = {
45           User = user;
46           Group = group;
47         };
48       };
50     mkServer =
51       { mpd, musicService }:
52       {
53         boot.kernelModules = [ "snd-dummy" ];
54         services.mpd = mpd;
55         systemd.services.musicService = musicService;
56       };
57   in
58   {
59     name = "mpd";
60     meta = {
61       maintainers = with lib.maintainers; [ emmanuelrosa ];
62     };
64     nodes = {
65       client = { ... }: { };
67       serverALSA =
68         { ... }:
69         lib.mkMerge [
70           (mkServer {
71             mpd = defaultMpdCfg // {
72               network.listenAddress = "any";
73               extraConfig = ''
74                 audio_output {
75                   type "alsa"
76                   name "ALSA"
77                   mixer_type "null"
78                 }
79               '';
80             };
81             musicService = musicService { inherit (defaultMpdCfg) user group musicDirectory; };
82           })
83           { networking.firewall.allowedTCPPorts = [ 6600 ]; }
84         ];
86       serverPulseAudio =
87         { ... }:
88         lib.mkMerge [
89           (mkServer {
90             mpd = defaultMpdCfg // {
91               extraConfig = ''
92                 audio_output {
93                   type "pulse"
94                   name "The Pulse"
95                 }
96               '';
97             };
99             musicService = musicService { inherit (defaultMpdCfg) user group musicDirectory; };
100           })
101           {
102             hardware.pulseaudio = {
103               enable = true;
104               systemWide = true;
105               tcp.enable = true;
106               tcp.anonymousClients.allowAll = true;
107             };
108             systemd.services.mpd.environment.PULSE_SERVER = "localhost";
109           }
110         ];
111     };
113     testScript = ''
114       mpc = "${lib.getExe pkgs.mpc} --wait"
116       # Connects to the given server and attempts to play a tune.
117       def play_some_music(server):
118           server.wait_for_unit("mpd.service")
119           server.succeed(f"{mpc} update")
120           _, tracks = server.execute(f"{mpc} ls")
122           for track in tracks.splitlines():
123               server.succeed(f"{mpc} add {track}")
125           _, added_tracks = server.execute(f"{mpc} playlist")
127           # Check we succeeded adding audio tracks to the playlist
128           assert len(added_tracks.splitlines()) > 0
130           server.succeed(f"{mpc} play")
132           _, output = server.execute(f"{mpc} status")
133           # Assure audio track is playing
134           assert "playing" in output
136           server.succeed(f"{mpc} stop")
139       play_some_music(serverALSA)
140       play_some_music(serverPulseAudio)
142       client.wait_for_unit("multi-user.target")
143       client.succeed(f"{mpc} -h serverALSA status")
145       # The PulseAudio-based server is configured not to accept external client connections
146       # to perform the following test:
147       client.fail(f"{mpc} -h serverPulseAudio status")
148     '';
149   }